-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest17.java
More file actions
48 lines (44 loc) · 1.35 KB
/
test17.java
File metadata and controls
48 lines (44 loc) · 1.35 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
package byteDance;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* Created by lizeyang on 2020/5/13.
* 二叉树非递归后序遍历
*/
public class test17 {
//时间复杂度O(n),额外空间复杂度O(2n)
public static List<Integer> test(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
stack1.push(root);
while (!stack1.isEmpty()) {
TreeNode node = stack1.pop();
stack2.push(node);
if (node.left != null) {
stack1.push(node.left);
}
if (node.right != null) {
stack1.push(node.right);
}
}
while (!stack2.isEmpty()) {
list.add(stack2.pop().val);
}
return list;
}
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) + " ");
}
}
}