Skip to content

Commit 79eee52

Browse files
authored
Merge pull request gzc426#337 from V1ncentzzZ/master
2018/12/04打卡
2 parents 47ae0b6 + 0509a99 commit 79eee52

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
101. 对称二叉树
2+
3+
给定一个二叉树,检查它是否是镜像对称的。
4+
5+
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
6+
7+
1
8+
/ \
9+
2 2
10+
/ \ / \
11+
3 4 4 3
12+
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
13+
14+
1
15+
/ \
16+
2 2
17+
\ \
18+
3 3
19+
20+
```
21+
/**
22+
* Definition for a binary tree node.
23+
* public class TreeNode {
24+
* int val;
25+
* TreeNode left;
26+
* TreeNode right;
27+
* TreeNode(int x) { val = x; }
28+
* }
29+
*/
30+
class Solution {
31+
public boolean isSymmetric(TreeNode root) {
32+
if(root == null) return true;
33+
return symmetricCheck(root.left,root.right);
34+
}
35+
36+
public boolean symmetricCheck(TreeNode node1,TreeNode node2){
37+
if(node1 == null && node2 == null) return true;
38+
if(node1 == null || node2 == null) return false;
39+
return (node1.val != node2.val) ? false : symmetricCheck(node1.left,node2.right) && symmetricCheck(node1.right,node2.left);
40+
41+
}
42+
}
43+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[102. 二叉树的层次遍历] (https://leetcode-cn.com/problems/binary-tree-level-order-traversal/)
2+
3+
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
4+
5+
例如:
6+
给定二叉树: [3,9,20,null,null,15,7],
7+
8+
3
9+
/ \
10+
9 20
11+
/ \
12+
15 7
13+
返回其层次遍历结果:
14+
15+
[
16+
[3],
17+
[9,20],
18+
[15,7]
19+
]
20+
21+
22+
```
23+
/**
24+
* Definition for a binary tree node.
25+
* public class TreeNode {
26+
* int val;
27+
* TreeNode left;
28+
* TreeNode right;
29+
* TreeNode(int x) { val = x; }
30+
* }
31+
*/
32+
// TODO 解题关键:传递树高度
33+
class Solution {
34+
public List<List<Integer>> levelOrder(TreeNode root) {
35+
List<List<Integer>> result = new ArrayList<>();
36+
if(root == null) return result;
37+
executor(root,result,0);
38+
return result;
39+
}
40+
41+
public void executor(TreeNode node,List<List<Integer>> result,int high){
42+
if(node == null) return;
43+
if(result.size() < high+1) result.add(new ArrayList<>());
44+
result.get(high).add(node.val);
45+
high++;
46+
executor(node.left,result,high);
47+
executor(node.right,result,high);
48+
}
49+
}
50+
```

0 commit comments

Comments
 (0)