Skip to content

Commit d97b38c

Browse files
author
Your Name
committed
二叉树前中后
1 parent f982332 commit d97b38c

File tree

7 files changed

+197
-14
lines changed

7 files changed

+197
-14
lines changed

note/0107/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ return its bottom-up level order traversal as:
3131

3232
## 思路 0
3333

34-
题意是从下往上按层遍历二叉树,每一层是从左到右,按层遍历,很明显,宽搜第一时间符合,因为是从下往上,所以插入的时候每次插到链表头即可。
34+
题意是从下往上按层遍历二叉树,每一层是从左到右,按层遍历,很明显,宽搜第一时间符合
35+
因为是从下往上,所以插入的时候每次插到链表头即可。
3536

3637
```java
3738
/**

progress.txt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
easy :
2-
列表操作 21 83
2+
列表操作 21 83 104
33

44
medium:
55

66
hard:
77

8+
可以分为基础的数据结构,比如数组、链表、栈、队列、二叉树、堆的使用
9+
这几种常见的数据结构的基础操作一定要很熟悉,
10+
比如链表逆置、删除、获取第 K 个元素、判断是否有环等,
11+
二叉树翻转、深度遍历、层级遍历、求树深度、公共父节点等。
12+
13+
已完成: 链表逆置, 删除, 获取第 K 个元素, 是否有环
14+
深度遍历, 求树深度,
15+
16+
未完成:二叉树翻转
17+
18+
算法是一定要复习的,在很多面试的过程中都会穿插算法题。面试的算法题一般不会很难,可以分为基础的数据结构
19+
比如数组、链表、栈、队列、二叉树、堆的使用,这几种常见的数据结构的基础操作一定要很熟悉
20+
比如链表逆置、删除、获取第 K 个元素、判断是否有环等,二叉树翻转、深度遍历、层级遍历、求树深度、公共父节点等。
21+
22+
23+
另一种是常见的搜索、排序算法,这两类算法出现频率很高,一定要知道它们常见的几种实现方式,比如排序方式有
24+
冒泡、快排、插入、归并、堆排序等。注意这里一定不要简单地去记忆算法实现,因为面试的时候可能不会直接
25+
让你写出对应的算法,会出一些使用搜索或者排序算法来实现的题目,
26+
这类题目你可以去 LeetCode 上通过标签过滤出来。
827

928

10-
可以分为基础的数据结构,比如数组、链表、栈、队列、二叉树、堆的使用,这几种常见的数据结构的基础操作一定要很熟悉,
11-
比如链表逆置、删除、获取第 K 个元素、判断是否有环等,二叉树翻转、深度遍历、层级遍历、求树深度、公共父节点等。

src/com/blankj/easy/_0083/Solution.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ public static ListNodeMe reverseList2(ListNodeMe current) {
109109
return pre;
110110
}
111111

112+
public static ListNodeMe reverseList3(ListNodeMe current) {
113+
ListNodeMe next = null;
114+
ListNodeMe pre = null;
115+
while (current != null) {
116+
// 保存next指针
117+
next = current.next;
118+
// 打断next指针,指向前一个节点
119+
current.next = pre;
120+
121+
// 为下一次循环做准备, 将pre节点指向当前节点, 将当前节点指向下一个节点,进入下一次循环
122+
pre = current;
123+
current = next;
124+
}
125+
return pre;
126+
}
127+
112128
/**
113129
* 删除第index个元素, 从0开始 "[1,2,3,4,5,6,8,10]" 2
114130
* @param current

src/com/blankj/easy/_0104/Solution.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33

44
import com.blankj.structure.TreeNode;
5+
import com.blankj.structure.TreeNodeMe;
6+
7+
import static java.lang.System.out;
58

69
/**
710
* <pre>
@@ -17,10 +20,44 @@ public int maxDepth(TreeNode root) {
1720
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
1821
}
1922

23+
public static void main_author(String[] args) {
24+
Solution solution = new Solution();
25+
// System.out.println(solution.maxDepth(TreeNode.createTestData("[]")));
26+
TreeNode rootNode = TreeNode.createTestData("[1,2,2,3,4,4,3]");
27+
int depth = solution.maxDepth(rootNode);
28+
out.println(depth);
29+
TreeNode.print(rootNode);
30+
// System.out.println(solution.maxDepth(TreeNode.createTestData("[9,-42,-42,null,76,76,null,null,13,null,13]")));
31+
}
32+
2033
public static void main(String[] args) {
2134
Solution solution = new Solution();
22-
System.out.println(solution.maxDepth(TreeNode.createTestData("[]")));
23-
System.out.println(solution.maxDepth(TreeNode.createTestData("[1,2,2,3,4,4,3]")));
24-
System.out.println(solution.maxDepth(TreeNode.createTestData("[9,-42,-42,null,76,76,null,null,13,null,13]")));
35+
// TreeNodeMe rootNode = TreeNodeMe.createTestData("[1,2,2,3,4,4,3,5,6]");
36+
TreeNodeMe rootNode = TreeNodeMe.createTestData("[0,1,2,3,4]");
37+
int depth = solution.maxDepthMe(rootNode);
38+
out.println(depth);
39+
// TreeNodeMe.print(rootNode);
40+
41+
}
42+
43+
/**
44+
* 深度搜索
45+
* 0 -> 1 -> 3 -> 3.left return 1 -> 3.right return 1-> max(1,1) ->
46+
* 1.left return 2 -> 4 -> 4.left is null return 1 -> 4.right is null return 1 ->
47+
* -> max(1, 1) - > 1.right return 2 -> max.(2, 2) return 2 ->
48+
* 0.left return 3 -> 2 -> 2.left -> 2.left return 1 -> 2.right -> 2.right return 1
49+
* -> max(1, 1) ->0.right return 2 -> max(3, 2) -> return 3
50+
* @param root
51+
* @return
52+
*/
53+
public int maxDepthMe(TreeNodeMe root) {
54+
if (root == null) {
55+
return 0;
56+
}
57+
out.println("经过了此节点值为:" +root.value);
58+
int leftDepth = maxDepthMe(root.left) + 1;
59+
int rightDepth = maxDepthMe(root.right) + 1;
60+
return Math.max(leftDepth, rightDepth);
2561
}
62+
2663
}

src/com/blankj/easy/_0107/Solution.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33

44
import com.blankj.structure.TreeNode;
5+
import com.blankj.structure.TreeNodeMe;
56

67
import java.util.LinkedList;
78
import java.util.List;
89

10+
import static java.lang.System.out;
11+
912
/**
1013
* <pre>
1114
* author: Blankj
@@ -17,24 +20,57 @@
1720
public class Solution {
1821
public List<List<Integer>> levelOrderBottom(TreeNode root) {
1922
List<List<Integer>> list = new LinkedList<>();
20-
helper(list, root, 0);
23+
helper2(list, root, 0);
2124
return list;
2225
}
2326

2427
private void helper(List<List<Integer>> list, TreeNode root, int level) {
2528
if (root == null) return;
26-
if (level >= list.size()) {
29+
if (level >= list.size()) { // 每经过一层添加一个list
2730
list.add(0, new LinkedList<>());
2831
}
2932
helper(list, root.left, level + 1);
3033
helper(list, root.right, level + 1);
3134
list.get(list.size() - level - 1).add(root.val);
3235
}
3336

37+
private void helper2(List<List<Integer>> list, TreeNode root, int level) {
38+
if (root == null) return;
39+
if (level >= list.size()) { // 每经过一层添加一个list
40+
list.add(0, new LinkedList<>());
41+
}
42+
helper2(list, root.left, level + 1);
43+
helper2(list, root.right, level + 1);
44+
out.print(root.val + " "); // 在这里控制(前中后,指的是中间节点的访问顺序)序
45+
list.get(list.size() - level - 1).add(root.val);
46+
}
47+
48+
public static void main2(String[] args) {
49+
Solution solution = new Solution();
50+
// System.out.println(solution.levelOrderBottom(TreeNode.createTestData("[]")));
51+
List<List<Integer>> lists = solution.levelOrderBottom(TreeNode.createTestData("[1,2,2,3,4,4,3]"));
52+
// out.println(lists);
53+
// System.out.println(solution.levelOrderBottom(TreeNode.createTestData("[9,-42,-42,null,76,76,null,null,13,null,13]")));
54+
}
55+
3456
public static void main(String[] args) {
3557
Solution solution = new Solution();
36-
System.out.println(solution.levelOrderBottom(TreeNode.createTestData("[]")));
37-
System.out.println(solution.levelOrderBottom(TreeNode.createTestData("[1,2,2,3,4,4,3]")));
38-
System.out.println(solution.levelOrderBottom(TreeNode.createTestData("[9,-42,-42,null,76,76,null,null,13,null,13]")));
58+
List<List<Integer>> lists = solution.levelOrderBottomMe(TreeNodeMe.createTestData("[1,2,2,3,4,4,3]"));
59+
// out.println(lists);
3960
}
61+
62+
public List<List<Integer>> levelOrderBottomMe(TreeNodeMe root) {
63+
List<List<Integer>> list = new LinkedList<>();
64+
helperMe(root);
65+
return list;
66+
}
67+
68+
private void helperMe(TreeNodeMe root) {
69+
if (root == null) return;
70+
out.print(root.value + " ");
71+
helperMe(root.left);
72+
helperMe(root.right);
73+
74+
}
75+
4076
}

src/com/blankj/structure/TreeNode.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ public static TreeNode createTestData(String data) {
2828
if (data.equals("[]")) return null;
2929
data = data.substring(1, data.length() - 1);
3030
String[] split = data.split(",");
31-
int len = len = split.length;
31+
int len = split.length;
32+
// 数组保存所有节点
3233
TreeNode[] treeNodes = new TreeNode[len];
33-
data = data.substring(1, data.length() - 1);
34+
// [1,2,2,3,4,4,3]
3435
for (int i = 0; i < len; i++) {
3536
if (!split[i].equals("null")) {
37+
// 给节点赋值, 但是没有引用关系
3638
treeNodes[i] = new TreeNode(Integer.valueOf(split[i]));
3739
}
3840
}
41+
// 建立引用关系
3942
for (int i = 0; i < len; i++) {
4043
if (treeNodes[i] != null) {
4144
int leftIndex = i * 2 + 1;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.blankj.structure;
2+
3+
public class TreeNodeMe {
4+
public int value;
5+
public TreeNodeMe left;
6+
public TreeNodeMe right;
7+
8+
public TreeNodeMe(int value) {
9+
this.value = value;
10+
}
11+
12+
public static TreeNodeMe createTestData(String data) {
13+
// [1,2,2,3,4,4,3]
14+
data = data.substring(1, data.length() - 1);
15+
String[] split = data.split(",");
16+
int len = split.length;
17+
TreeNodeMe treeNodes[] = new TreeNodeMe[len];
18+
// 给数组赋值
19+
for (int i = 0; i < len; i++) {
20+
treeNodes[i] = new TreeNodeMe(Integer.parseInt(split[i])); // 异常未捕获
21+
}
22+
// 建立引用关系
23+
for (int i = 0; i < len; i++) {
24+
int leftIndex = i * 2 + 1;
25+
int rightIndex = leftIndex + 1;
26+
if (leftIndex < len) {
27+
treeNodes[i].left = treeNodes[leftIndex];
28+
}
29+
if (rightIndex < len) {
30+
treeNodes[i].right = treeNodes[rightIndex];
31+
}
32+
}
33+
return treeNodes[0];
34+
}
35+
36+
private static final String space = " ";
37+
38+
/**
39+
* 竖向打印二叉树
40+
*
41+
* @param root 二叉树根节点
42+
*/
43+
public static void print(TreeNodeMe root) {
44+
print(root, 0);
45+
}
46+
47+
private static void print(TreeNodeMe node, int deep) {
48+
if (node == null) {
49+
printSpace(deep);
50+
System.out.println("#");
51+
return;
52+
}
53+
print(node.right, deep + 1);
54+
printSpace(deep);
55+
printNode(node.value);
56+
print(node.left, deep + 1);
57+
}
58+
59+
private static void printSpace(int count) {
60+
for (int i = 0; i < count; i++) {
61+
System.out.printf(space);
62+
}
63+
}
64+
65+
private static void printNode(int val) {
66+
StringBuilder res = new StringBuilder(val + "<");
67+
int spaceNum = space.length() - res.length();
68+
for (int i = 0; i < spaceNum; i++) {
69+
res.append(" ");
70+
}
71+
System.out.println(res);
72+
}
73+
}

0 commit comments

Comments
 (0)