Wednesday, 23 December 2015

LeetCode Symmetric Tree

LeetCode Symmetric Tree

/**

 * Definition for a binary tree node.

 * struct TreeNode {

 *     int val;

 *     TreeNode *left;

 *     TreeNode *right;

 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}

 * };

 */

class Solution {

public:

    bool fun(TreeNode* root1,TreeNode* root2){

        if(root1==NULL && root2==NULL)

         return true;

        if(root1==NULL || root2==NULL)

         return false;

        return (root1->val==root2->val && fun(root1->left,root2->right) && fun(root1->right,root2->left));

    }

    bool isSymmetric(TreeNode* root) {

        if(root==NULL)

          return true;

         return fun(root->left,root->right);

       

    }

};

No comments:

Post a Comment