File tree 3 files changed +101
-4
lines changed
3 files changed +101
-4
lines changed Original file line number Diff line number Diff line change @@ -72,11 +72,11 @@ public static void main(String[] args) throws IOException {
72
72
String op = str [0 ];
73
73
int k , x ;
74
74
if (op .equals ("L" )) {
75
- // 在左端插入就是在0号点的右边插入
75
+ // 在最左端插入就是在0号点的右边插入
76
76
x = Integer .parseInt (str [1 ]);
77
77
add (0 , x );
78
78
} else if (op .equals ("R" )) {
79
- // 在右端插入就是在1号点的左边插入
79
+ // 在最右端插入就是在1号点的左边插入
80
80
x = Integer .parseInt (str [1 ]);
81
81
add (l [1 ], x );
82
82
} else if (op .equals ("D" )) {
@@ -97,11 +97,11 @@ public static void main(String[] args) throws IOException {
97
97
}
98
98
99
99
// 注意这个遍历的开始是从0的右指针指向的结点开始!!!!!不是从i=0开始
100
- for (int i = r [0 ]; i != 1 ; i = r [i ])
100
+ for (int i = r [0 ]; i != 1 ; i = r [i ]) //从开始结点0一直遍历到最右端点1
101
101
System .out .print (e [i ] + " " );
102
102
}
103
103
104
- // 0号点表示左端点;1号点表示右端点
104
+ // 初始就有的两个: 0号点表示左端点;1号点表示右端点
105
105
public static void init () {
106
106
r [0 ] = 1 ;
107
107
l [1 ] = 0 ;
Original file line number Diff line number Diff line change
1
+ package offer ;
2
+
3
+ import java .util .LinkedList ;
4
+ import java .util .List ;
5
+
6
+ /**
7
+ * @author : CodeWater
8
+ * @create :2022-07-10-12:10
9
+ * @Function Description :55.1 二叉树的深度
10
+ */
11
+ public class _55_1TheDepthOfTheBinaryTree {
12
+ /**
13
+ * Definition for a binary tree node.
14
+ * public class TreeNode {
15
+ * int val;
16
+ * TreeNode left;
17
+ * TreeNode right;
18
+ * TreeNode(int x) { val = x; }
19
+ * }
20
+ */
21
+ //==========================bfs============================
22
+ class Solution {
23
+ public int maxDepth (TreeNode root ) {
24
+ if ( root == null ) return 0 ;
25
+ // queue本层结点, temp下一层结点
26
+ List <TreeNode > queue = new LinkedList <>(){{ add (root ); }} , temp ;
27
+ int res = 0 ;
28
+ while ( queue .size () != 0 ){
29
+ temp = new LinkedList <>();
30
+ for ( TreeNode node : queue ){
31
+ // 扫描本层结点的孩子结点,添加到temp中
32
+ if ( node .left != null ) temp .add (node .left );
33
+ if ( node .right != null ) temp .add (node .right );
34
+ }
35
+ // 队列换成下一层的结点
36
+ queue = temp ;
37
+ res ++;
38
+ }
39
+ return res ;
40
+ }
41
+ }
42
+
43
+
44
+ //==========================递归后序遍历============================
45
+ /**
46
+ * Definition for a binary tree node.
47
+ * public class TreeNode {
48
+ * int val;
49
+ * TreeNode left;
50
+ * TreeNode right;
51
+ * TreeNode(int x) { val = x; }
52
+ * }
53
+ */
54
+ class Solution2 {
55
+ public int maxDepth (TreeNode root ) {
56
+ if ( root == null ) return 0 ;
57
+ return Math .max ( maxDepth (root .left ) , maxDepth (root .right ) ) + 1 ;
58
+ }
59
+ }
60
+ }
Original file line number Diff line number Diff line change
1
+ package offer ;
2
+
3
+ /**
4
+ * @author : CodeWater
5
+ * @create :2022-07-10-12:30
6
+ * @Function Description :55.2 平衡二叉树
7
+ */
8
+ public class _55_2BalanceBinaryTree {
9
+ /**
10
+ * Definition for a binary tree node.
11
+ * public class TreeNode {
12
+ * int val;
13
+ * TreeNode left;
14
+ * TreeNode right;
15
+ * TreeNode(int x) { val = x; }
16
+ * }
17
+ */
18
+ class Solution {
19
+ // 判断平衡二叉树:树的深度是左右子树最大深度+1----> 左右子树高度查不能超过2
20
+ public boolean isBalanced (TreeNode root ) {
21
+ // 不等于-1时true,是平衡二叉树
22
+ return recursion (root ) != -1 ;
23
+ }
24
+
25
+ // 0:越过叶子结点 -1:不是平衡二叉树
26
+ public int recursion (TreeNode root ){
27
+ if ( root == null ) return 0 ;
28
+ int left = recursion (root .left );
29
+ // 剪枝,如果子树left是-1,就说明不是平衡二叉树,直接返回
30
+ if ( left == -1 ) return -1 ;
31
+ int right = recursion ( root .right );
32
+ if ( right == - 1 ) return -1 ;
33
+ // 左右字数高度差大于2,不是; 小于2,用高的加1向上返回
34
+ return Math .abs ( left - right ) >= 2 ? -1 : Math .max ( left , right ) + 1 ;
35
+ }
36
+ }
37
+ }
You can’t perform that action at this time.
0 commit comments