Skip to content

Commit 20d8cd9

Browse files
committed
Merge pull request Hearen#103 from cxxly/chenxiaoxu
Chenxiaoxu
2 parents c64e341 + 73cb2c2 commit 20d8cd9

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

ChenXiaoxu/101. Symmetric Tree.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# 101. Symmetric Tree
2+
3+
## Problem
4+
5+
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
6+
7+
For example, this binary tree is symmetric:
8+
```
9+
1
10+
/ \
11+
2 2
12+
/ \ / \
13+
3 4 4 3
14+
But the following is not:
15+
1
16+
/ \
17+
2 2
18+
\ \
19+
3 3
20+
```
21+
Note:
22+
Bonus points if you could solve it both recursively and iteratively.
23+
24+
tag:
25+
26+
## Solution
27+
28+
### 递归
29+
30+
树中两个点对称(n1,n2), 递归判断n1的左子树与n2的右子树,n2的右子树与n2的左子树是否对称
31+
32+
**java**
33+
```java
34+
public boolean isSymmetric(TreeNode root) {
35+
return root==null || helper(root.left, root.right);
36+
}
37+
38+
boolean helper(TreeNode n1, TreeNode n1) {
39+
if(n1==null || n2==null) return n1==n2;
40+
if(n1.val != n2.val) return false;
41+
return helper(n1.left, n2.right)&&helper(n1.right, n2.left);
42+
}
43+
```
44+
45+
**go**
46+
```go
47+
48+
```
49+
50+
### 迭代
51+
52+
**模拟递归工作栈**
53+
54+
```java
55+
public boolean isSymmetric(TreeNode root) {
56+
if(root==null) return true;
57+
58+
Stack<TreeNode> s = new Stack<TreeNode>();
59+
60+
if(root.left!=null && root.right!=null) {
61+
s.push(root.left);
62+
s.push(root.right);
63+
} else if(root.left==null && root.right==null) {
64+
return true;
65+
} else {
66+
return false;
67+
}
68+
69+
TreeNode left, right;
70+
71+
while(!s.isEmpty()) {
72+
// if(s.size()%2!=0) return false;
73+
right = s.pop();
74+
left = s.pop();
75+
if(right.val!=left.val) return false;
76+
77+
if(left.left!=null && right.right!=null) {
78+
s.push(left.left);
79+
s.push(right.right);
80+
} else if(!(left.left==null&&right.right==null)) {
81+
return false;
82+
}
83+
84+
if(left.right!=null && right.left!=null) {
85+
s.push(left.right);
86+
s.push(right.left);
87+
} else if(!(left.right==null && right.left==null)) {
88+
return false;
89+
}
90+
}
91+
92+
return true;
93+
}
94+
```
95+
96+
**层次遍历,判断每层是否对称**
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 226. Invert Binary Tree
2+
3+
## Problem
4+
nvert a binary tree.
5+
6+
4
7+
/ \
8+
2 7
9+
/ \ / \
10+
1 3 6 9
11+
to
12+
4
13+
/ \
14+
7 2
15+
/ \ / \
16+
9 6 3 1
17+
18+
tag:
19+
20+
## Solution
21+
22+
>如果节点为空,返回
23+
>否则
24+
- 递归转换左子树
25+
- 递归转换右子树
26+
- 对换左右子树
27+
28+
**java**
29+
```java
30+
public TreeNode invertTree(TreeNode root) {
31+
if(root==null) return root;
32+
TreeNode tmp = root.left;
33+
root.left = invertTree(root.right);
34+
root.right = invertTree(tmp);
35+
return root;
36+
}
37+
```
38+
39+
**go**
40+
```go
41+
42+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 257. Binary Tree Paths
2+
3+
## Problem
4+
5+
Given a binary tree, return all root-to-leaf paths.
6+
7+
For example, given the following binary tree:
8+
9+
1
10+
/ \
11+
2 3
12+
\
13+
5
14+
All root-to-leaf paths are:
15+
16+
["1->2->5", "1->3"]
17+
18+
tag:
19+
- Tree
20+
21+
## Solution
22+
23+
**java**
24+
```java
25+
public List<String> binaryTreePaths(TreeNode root) {
26+
List<String> res = new ArrayList<String>();
27+
helper(res, new StringBuilder(), root);
28+
return res;
29+
}
30+
31+
void helper(List<String> res, StringBuilder str, TreeNode root) {
32+
if(root==null) return;
33+
int len = str.length();
34+
str.append(root.val);
35+
if(root.left==null && root.right==null) {
36+
res.add(str.toString());
37+
} else {
38+
str.append("->");
39+
helper(res, str, root.left);
40+
helper(res, str, root.right);
41+
}
42+
str.setLength(len);
43+
}
44+
```
45+
46+
**go**
47+
```go
48+
49+
```

0 commit comments

Comments
 (0)