Skip to content

Commit f7c9fda

Browse files
authored
Merge pull request gzc426#320 from u-hechongyang/master
3
2 parents 7a72c9d + 80b5d61 commit f7c9fda

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

2018.12.04-leetcode101/阳.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public boolean isSymmetric(TreeNode root) {
12+
if(root == null) {
13+
return true;//根节点为空,返回true
14+
}else {
15+
return isSymmetric(root.left, root.right);
16+
}
17+
}
18+
public boolean isSymmetric(TreeNode pleft, TreeNode pright) {
19+
if(pleft == null && pright == null) {
20+
return true;//说明只有一个根节点,返回true
21+
}
22+
if(pleft == null || pright == null) {
23+
return false;//一个子树为空,另一个子树不为空,返回false
24+
}
25+
//都不为空的情况下判断是否相等
26+
if(pleft.val != pright.val) {
27+
return false;
28+
}
29+
//继续判断左子树与右子树是否对称相等
30+
return isSymmetric(pleft.left, pright.right)&&isSymmetric(pleft.right, pright.left);
31+
}
32+
}

0 commit comments

Comments
 (0)