LeetCode 之 Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
/ \
2   2
/ \ / \
3  4 4  3


But the following is not:

    1
/ \
2   2
\   \
3    3

1 如果node1和node2都为null，对称

2 如果node1和node2都不为null，如果node1->val == node2->val，还不一定对称，需要判定node1的左和node2的右是否对称；node1的右和node2的左是否对称。

3 如果node1和node2有一个不为null，则不对称

class Solution {
public:
bool isSymmetric(TreeNode *root) {
if(!root) return true;
return two_compare(root->left , root->right);
}

bool two_compare(TreeNode * leftTree , TreeNode * rightTree){

if(leftTree && rightTree){
if(leftTree->val != rightTree->val){
return false;
}
return ( two_compare(leftTree->left,rightTree->right) && two_compare(leftTree->right,rightTree->left) );
}
else if(!leftTree && !rightTree){
return true;
}
else {
return false;
}

}

};

【LeetCode】101. Symmetric Tree 解题报告

2016-05-30 23:06:35

Symmetric Tree -- LeetCode

2014-04-07 04:52:23

LeetCode(101)Symmetric Tree

2014-01-17 06:48:54

【leetcode c++】101 Symmetric Tree

2015-07-27 14:39:02

Leetcode-Symmetric Tree——判断二叉树是否对称

2014-04-20 01:00:22

Leetcode Symmetric Tree 递归和非递归解法

2014-02-16 01:11:36

【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree（对称树）】

2015-08-07 07:29:25

101. Symmetric Tree Leetcode Python

2015-01-30 07:28:30

leetcode 101. Symmetric Tree BFS广度优先遍历+DFS深度优先遍历

2017-09-11 22:10:18

leetcode上令人拍案叫绝的解法

2017-03-27 22:17:54

不良信息举报

LeetCode 之 Symmetric Tree