Symmetric Tree


  • 0

    Click here to see the full article post


  • 0
    T

    Can we do it with DFS? I think it is the same as the second solution other than the fact that DFS uses stacks.
    To be more specific, have two pointers(or stacks) traverse down left and right children. One will do post-order-traversal on the left and the other will do pre-order-traversal.


  • 0

    You mean doing DFS iteratively? Yes. Just change the queue to a stack and it will do DFS.


  • 0
    T

    Can we do it with DFS? I think it is the same as the second solution other than the fact that DFS uses stacks.


  • 0
    C

    I think in the first recursive solution, it traverses the entire tree twice, if you call mirror function in this way: isMirror(root, root);


  • 0
    K

    Even in iterative case,i think it will traverse the tree twice.


  • 0
    L

    The avoid traverse the tree twice in the first recursive solution, just use following in isSymmetric:
    if (root == null)
    return true;
    return dfs(root.left, root.right);


  • 0
    L

    I think preorder traversal can also solve it,but why I can not pass when input is [1]?My IDEA print "true" but oj print "false"


  • 0
    Z

    The second iterative solution do unnecessary push and pop twice since first push(root) twice.
    Here can just push(root->left) & posh(root->right) to avoid this, however, we need add extra judgement of whether root is NULL first


  • 0
    R

  • 0
    D

    Another approach is to take a preorder and postorder traversal of the tree and compare the lists returned by each. If equal then true, else false.


  • 0
    A

    Inorder traversal of val, level should be symmetric.


  • -1
    K

    if inorder traversal is done then for some base cases, it is showing error


  • 0
    R

    Traverse the tree with 2 pointers, one traverses to the left first, the other one traverses to the right first, and compare the two along the way,
    if there is any mismatch (value or pointer) the tree is not symmetric.


  • 0
    S

    About the space complexity: can you provide an example which explicitly shows the worse case complexity of O(n) in both approaches. For the recursive approach, I do not think that a linear tree will result in O(n) space complexity, because code with stop at the first level.


  • 0
    H

    Even in the implementations that permit it, null should not be inserted into a Queue, as null is also used as a special return value by the poll method to indicate that the queue contains no elements.
    https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html


  • 0
    W
    bool isSymmetric(TreeNode* root) {
        if(root == NULL){
            return true;
        }
        queue<TreeNode*> lq;
        queue<TreeNode*> rq;
        if(root->left){
            lq.push(root->left);
        }
        if(root->right){
            rq.push(root->right);
        }
        while(lq.size() && rq.size()){
            TreeNode *l = lq.front();
            TreeNode *r = rq.front();
            lq.pop();
            rq.pop();
            
            if(l->val != r->val)
                return false;
            if(l->left && r->right){
                lq.push(l->left);
                rq.push(r->right);
            }else if(!l->left && !r->right){
                
            }else{
                return false;
            }
            if(l->right && r->left){
                lq.push(l->right);
                rq.push(r->left);
            }else if(!l->right && !r->left){
                
            }else{
                return false;
            }
               
        }
        if(lq.size() != rq.size()){
            return false;
        }
        return true;
        
    }

  • 0
    H

    class Solution {
    public boolean isSymmetric(TreeNode root) {
    if (root==null) {
    return true;
    } else {
    return isSymmetricInside(root.left, root.right);
    }
    }

    public boolean isSymmetricInside(TreeNode p, TreeNode q) {
        if (p==null && q==null) {
            return true;
        }
        if (p!=null && q!=null) {
            return p.val==q.val && isSymmetricInside(p.left, q.right) && isSymmetricInside(p.right, q.left);
        }
        return false;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.