101. Symmetric Tree

Posted 我的名字叫周周

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了101. Symmetric Tree相关的知识,希望对你有一定的参考价值。

    /*
     * 101. Symmetric Tree 
     * 2016-5-16 By Mingyang 
     * 刚开始的时候只有一个root做不行,后面改过来,下面是标准的英文回答时间复杂度和空间复杂度的表述
     * Because we traverse the entire input tree once, the total run time is O(n)O(n), 
     * where nn is the total number of nodes in the tree.
     * The number of recursive calls is bound by the height of the tree. 
     * In the worst case, the tree is linear and the height is in O(n)O(n). 
     * Therefore, space complexity due to recursive calls on the stack is O(n)O(n) in the worst case.
     * 当然我们可以用擅长的queue来做
     */
    public boolean isSymmetric(TreeNode root) {
        if (root == null)
            return true;
        return isSymmetricTree(root.left, root.right);
    }
    public boolean isSymmetricTree(TreeNode p, TreeNode q) {
        if (p == null && q == null)
            return true;
        if (p == null || q == null)
            return false;
        return (p.val == q.val) && isSymmetricTree(p.left, q.right)&& isSymmetricTree(p.right, q.left);
    }

 

以上是关于101. Symmetric Tree的主要内容,如果未能解决你的问题,请参考以下文章

101. Symmetric Tree

[LeetCode]题解(python):101-Symmetric Tree

101. Symmetric Tree

101. Symmetric Tree

LeetCode OJ 101Symmetric Tree

101. Symmetric Tree