【leetcode】101-Symmetric Tree

problem

Symmetric Tree

code

回归方法

/**
 * 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 isSymmetric(TreeNode* root) {
        if(root==NULL) return true;//
        return isMirro(root->left, root->right);        
    }
    bool isMirro(TreeNode* t1, TreeNode* t2)
    {
        if(t1==NULL && t2==NULL) return true;
        if(t1==NULL || t2==NULL) return false;
        return (t1->val==t2->val) && isMirro(t1->right, t2->left) && isMirro(t1->left, t2->right);
    }
};

 

 

参考

1.Symmetric Tree;

2.leetcode_discuss;

3. leetcode_solution;

posted on 2018-11-25 19:32  鹅要长大  阅读(126)  评论(0编辑  收藏  举报

导航