Skip to content

Commit 9f4f187

Browse files
authored
Merge pull request gzc426#9 from lyfYangTing/lyfYangTing-patch-1
对称二叉树
2 parents ea7ab9a + 38f826b commit 9f4f187

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

2018.12.04-leetcode101/杨.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
```
2+
/**
3+
* 如果一个树的左子树与右子树镜像对称,那么这个树是对称的。
4+
* 如果同时满足下面的条件,两个树互为镜像:
5+
* 1.它们的两个根结点具有相同的值。
6+
* 2.每个树的右子树都与另一个树的左子树镜像对称。
7+
*/
8+
public boolean isSymmetric(TreeNode root) {
9+
if(root == null ){
10+
return true;
11+
}
12+
return isMirrorSymmetry(root.left,root.right);
13+
}
14+
15+
public boolean isMirrorSymmetry(TreeNode left,TreeNode right){
16+
if(left==null && right==null){
17+
return true;
18+
}
19+
if(left==null || right==null){
20+
return false;
21+
}
22+
return left.val==right.val && isMirrorSymmetry(left.left,right.right) && isMirrorSymmetry(left.right,right.left);
23+
}
24+
25+
```

0 commit comments

Comments
 (0)