Skip to content

Latest commit

 

History

History
38 lines (31 loc) · 1.01 KB

symmetric-tree.md

File metadata and controls

38 lines (31 loc) · 1.01 KB

Solution

    /**
    * Definition for a binary tree node.
    * struct TreeNode {
    *     int val;
    *     TreeNode *left;
    *     TreeNode *right;
    *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
    *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    * };
    */
    class Solution {
    public:
        bool isMirror(TreeNode* A, TreeNode* B) {
            if(A == NULL && B == NULL)
                return true;
            if(A == NULL || B == NULL)
                return false;
            return A -> val == B -> val &&  isMirror(A -> left, B -> right) 
                && isMirror(A -> right, B -> left);
                
        }
        bool isSymmetric(TreeNode* root) {
            return isMirror(root, root);
        }
    };