Skip to content

Commit bac508e

Browse files
committed
提交94,145,144,104
1 parent dbfcebb commit bac508e

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.mistray.tree;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.tree
7+
* @create 2020年03月06日 16:26
8+
* @Desc
9+
*/
10+
public class BalancedBinaryTree110 {
11+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.mistray.tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
/**
8+
* @author ZJY(MistRay)
9+
* @Project algorithm-study
10+
* @Package com.mistray.tree
11+
* @create 2020年03月06日 13:54
12+
* @Desc
13+
*/
14+
public class BinaryTreeInorderTraversal94 {
15+
16+
17+
public static void main(String[] args) {
18+
TreeNode root = new TreeNode(1);
19+
root.right = new TreeNode(2);
20+
root.right.left = new TreeNode(3);
21+
BinaryTreeInorderTraversal94 bin = new BinaryTreeInorderTraversal94();
22+
List<Integer> integers = bin.inorderTraversal(root);
23+
24+
}
25+
26+
public List<Integer> inorderTraversal(TreeNode root) {
27+
List<Integer> list = new ArrayList<>();
28+
Stack<TreeNode> stack = new Stack<>();
29+
while (stack.size() > 0 || root != null) {
30+
if (root != null) {
31+
stack.add(root);
32+
root = root.left;
33+
} else {
34+
TreeNode pop = stack.pop();
35+
list.add(pop.val);
36+
root = pop.right;
37+
}
38+
}
39+
return list;
40+
}
41+
42+
public List<Integer> inorderTraversal2(TreeNode root) {
43+
List<Integer> list = new ArrayList<>();
44+
helper(root, list);
45+
return list;
46+
}
47+
48+
49+
public void helper(TreeNode root, List<Integer> list) {
50+
if (root == null) {
51+
return;
52+
}
53+
helper(root.left, list);
54+
list.add(root.val);
55+
helper(root.right, list);
56+
}
57+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.mistray.tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
/**
8+
* @author ZJY(MistRay)
9+
* @Project algorithm-study
10+
* @Package com.mistray.tree
11+
* @create 2020年03月06日 14:47
12+
* @Desc
13+
*/
14+
public class BinaryTreePostorderTraversal145 {
15+
public List<Integer> postorderTraversal(TreeNode root) {
16+
List<Integer> list = new ArrayList<>();
17+
Stack<TreeNode> stack1 = new Stack<>();
18+
Stack<TreeNode> stack2 = new Stack<>();
19+
stack1.add(root);
20+
while (stack1.size() > 0) {
21+
TreeNode pop = stack1.pop();
22+
stack2.add(pop);
23+
if (pop.left != null) {
24+
stack1.add(pop.left);
25+
}
26+
if (pop.right != null) {
27+
stack1.add(pop.right);
28+
}
29+
}
30+
31+
while (stack2.size() > 0) {
32+
list.add(stack2.pop().val);
33+
}
34+
return list;
35+
}
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.mistray.tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author ZJY(MistRay)
8+
* @Project algorithm-study
9+
* @Package com.mistray.tree
10+
* @create 2020年03月06日 14:42
11+
* @Desc
12+
*/
13+
public class BinaryTreePreorderTraversal144 {
14+
15+
16+
public List<Integer> preorderTraversal(TreeNode root) {
17+
List<Integer> list = new ArrayList<>();
18+
helper(root,list);
19+
return list;
20+
}
21+
22+
public void helper(TreeNode node, List<Integer> list) {
23+
if (node == null){
24+
return;
25+
}
26+
list.add(node.val);
27+
helper(node.left,list);
28+
helper(node.right,list);
29+
}
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.mistray.tree;
2+
3+
/**
4+
* @author ZJY(MistRay)
5+
* @Project algorithm-study
6+
* @Package com.mistray.tree
7+
* @create 2020年03月06日 11:39
8+
* @Desc
9+
*/
10+
public class MaximumDepthOfBinaryTree104 {
11+
12+
public int maxDepth(TreeNode root) {
13+
if (root == null) {
14+
return 0;
15+
}
16+
return max(root, 0, 0);
17+
}
18+
19+
// 获取当前树的最大深度
20+
public int max(TreeNode node, int level, int maxLevel) {
21+
// 当前深度+1
22+
int curLevel = level + 1;
23+
// 当前深度如果比最大深度要大的话,更新最大深度
24+
if (curLevel > maxLevel) {
25+
maxLevel = curLevel;
26+
}
27+
// 如果当前节点的左节点不为空的话递归,直到左节点的尽头
28+
if (node.left != null) {
29+
maxLevel = max(node.left, curLevel, maxLevel);
30+
}
31+
// 第一次执行时到这里时,为左节点的尽头,已经没有更左的节点
32+
if (node.right != null) {
33+
maxLevel = max(node.right, curLevel, maxLevel);
34+
}
35+
return maxLevel;
36+
}
37+
38+
39+
}

0 commit comments

Comments
 (0)