1.基础算法💡
1.1.前序遍历
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Deque<TreeNode> stack = new LinkedList<>();
TreeNode p = root;
while(p!=null||!stack.isEmpty()){
if(p!=null){
stack.push(p);
res.add(p.val); //***** 和中序 遍历就是这里不同
p=p.left;
}else{
p = stack.pop();
p=p.right;
}
}
return res;
}
1.2.中序遍历
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Deque<TreeNode> stack = new LinkedList<>();
TreeNode p = root;
while(p!=null||!stack.isEmpty()){
if(p!=null){
stack.push(p);
p=p.left;
}else{
p = stack.pop();
res.add(p.val); //***** 和前序遍历就是这里不同
p=p.right;
}
}
return res;
}
1.3.后序遍历
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Deque<TreeNode> stack = new LinkedList<>();
TreeNode curr = root;
TreeNode pre = null;
while(curr!=null||!stack.isEmpty()){
if(curr!=null){
stack.push(curr);
curr=curr.left;
}else{
curr = stack.peek();
if(curr.right==null||curr.right==pre){
res.add(curr.val);//
stack.poll();
pre = curr;
curr = null;
}else{
curr = curr.right;
}
}
}
return res;
}
1.4.层序遍历
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
if(root==null) return res;
Deque<TreeNode> queue = new LinkedList<>();//辅助数据结构-队列
queue.add(root);
while(!queue.isEmpty()){
List<Integer> list =new ArrayList<>();
int count = queue.size();
for(int i=1;i<=count;i++){
TreeNode node = queue.poll();
list.add(node.val);
if(node.left!=null){
queue.add(node.left);
}
if(node.right!=null){
queue.add(node.right);
}
}
res.add(list);
}
return res;
}
}
2.适用题型🎯
1
3.注意点❗
1
2
1️⃣注意递归结束的条件❗
2️⃣选择合适的辅助数据结构❗-栈/队列
4.模板🔑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//前中后序模板
public int Method() {
List<Integer> res = new ArrayList<>();
Deque<TreeNode> stack = new LinkedList<>();//栈
TreeNode p = root;
while(p!=null||!stack.isEmpty()){
if(p!=null){
stack.push(p);
//前序-这里加入结果集
p=p.left;
}else{
p = stack.pop();
//中序-这里加入结果集
p=p.right;
}
}
}
//层序遍历模板
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
if(root==null) return res;
Deque<TreeNode> queue = new LinkedList<>();//辅助数据结构-队列
queue.add(root);
while(!queue.isEmpty()){
List<Integer> list =new ArrayList<>();
int count = queue.size();
for(int i=1;i<=count;i++){
TreeNode node = queue.poll();
list.add(node.val);
if(node.left!=null){
queue.add(node.left);
}
if(node.right!=null){
queue.add(node.right);
}
}
res.add(list);
}
return res;
}