4ms c++: invert half of the tree and check if left and right equal.


  • 0
    H

    I used solution of two other questions. Invert binary tree and same binary tree

    class Solution {
    public:
        TreeNode* invertTree(TreeNode* root) {
            if (!root) return root;
            invertTree(root->left);
            invertTree(root->right);
            swap(root->left,root->right);
            return root;
        }
        bool isSameTree(TreeNode* p, TreeNode* q) {
            if ((p==NULL)!=(q==NULL)) return false;
            if (p==NULL&&q==NULL) return true;
            if (p->val!=q->val) return false;
            if (isSameTree(p->left,q->left)&&isSameTree(p->right,q->right))
                return true;
            else
                return false;
        }
        bool isSymmetric(TreeNode* root) {
            if(!root) return true;
            invertTree(root->left);
            return isSameTree(root->left,root->right);
        }
    };

Log in to reply
 

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