forked from lizeyang18/byteDanceAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest36.java
More file actions
67 lines (60 loc) · 1.98 KB
/
test36.java
File metadata and controls
67 lines (60 loc) · 1.98 KB
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
66
67
package byteDance;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lizeyang on 2020/5/15.
* 二叉树右视图
*/
public class test36 {
//非递归写的太多了,多写写递归的,万一面试官让你都写呢——层次遍历实现
static List<List<Integer>> res = new ArrayList<>();
static List<Integer> list = new ArrayList<>();
public static List<Integer> test(TreeNode root) {
if (root == null) {
return list;
}
int depth = getDepth(root);
for (int i = 0; i < depth; i++) {
res.add(new ArrayList<>());
}
reverseNodeAtLevel(root, 0, res);
for (int i = 0; i < res.size(); i++) {
list.add(res.get(i).get(res.get(i).size() - 1));
}
return list;
}
public static void reverseNodeAtLevel(TreeNode root, int level, List<List<Integer>> res) {
if (root == null) {
return;
}
res.get(level).add(root.val);
reverseNodeAtLevel(root.left, level + 1, res);
reverseNodeAtLevel(root.right, level + 1, res);
}
//求最大深度,递归实现,递归赛高!
public static int getDepth(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null) {
return getDepth(root.right) + 1;
}
if (root.right == null) {
return getDepth(root.left) + 1;
}
return Math.max(getDepth(root.left), getDepth(root.right)) + 1;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(5);
root.left = new TreeNode(4);
root.right = new TreeNode(8);
root.left.left = new TreeNode(3);
root.right.left = new TreeNode(7);
root.right.left.left = new TreeNode(6);
root.right.right = new TreeNode(9);
List<Integer> res = test(root);
for (int i = 0; i < res.size(); i++) {
System.out.print(res.get(i) + " ");
}
}
}