forked from terrytong0876/LintCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Tree Preorder Traversal.java
executable file
·172 lines (141 loc) · 4.06 KB
/
Binary Tree Preorder Traversal.java
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
E
Preorder 写写, stack
1. Divide and conquer
2. Stack(NON-recursive) push curr, push right, push left.
3. recursive with helper method
```
/*
Given a binary tree, return the preorder traversal of its nodes' values.
Note
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,2,3].
Example
Challenge
Can you do it without recursion?
Tags Expand
Tree Binary Tree
//Recommend way: using a stack
//Recursive way can be seen here: http://www.ninechapter.com/solutions/binary-tree-preorder-traversal/
*/
/*
Recap: 12.08.2015
Draw a few nodes and will realize to use stack
Cannot use queue, because whatever added on it first, will first process.
That means if we add curr,left,right; they will be processed first... but we want to traverse all left nodes first.
IN FACT: binary tree traversal are all using stack...
*/
//Itereative
public class Solution {
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> rst = new ArrayList<Integer>();
if (root == null) {
return rst;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (node != null) {
rst.add(node.val);
stack.push(node.right);
stack.push(node.left);
}
}
return rst;
}
}
//recursive
public class Solution {
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> rst = new ArrayList<Integer>();
if (root == null) {
return rst;
}
helper(rst, root);
return rst;
}
public void helper(ArrayList<Integer>rst, TreeNode node){
if (node != null) {
rst.add(node.val);
helper(rst, node.left);
helper(rst, node.right);
}
}
}
/*
Thinking process:
Check if root is null
use a container to save results
use current node
put right on stack
put left on stack
4. In next run, the ‘left’ will be on top of stack, and will be taken first. So the order becomes: parent -> left -> right
*/
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Preorder in ArrayList which contains node values.
*/
public ArrayList<Integer> preorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
ArrayList<Integer> result = new ArrayList<Integer>();
//Check top
if (root == null) {
return result;
}
//save root
stack.push(root);
//add to result, and load on stack. Right-side at the bottom
while (!stack.empty()) {
TreeNode node = stack.pop();
result.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}//while
return result;
}
}
//Divide and Conquer - recursive
/*
Check root == null?
Dive them into 2 recursive calls: get result from left, get result from right
Conquer - add all of the results together and return. As the pre-order defines:
add current parent
add left nodes
add right nodes.
*/
//Divide and conquer
public ArrayList<Integer> preorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> result = new ArrayList<Integer>();
if (root == null) {
return result;
}
ArrayList<Integer> left = preorderTraversal(root.left);
ArrayList<Integer> right = preorderTraversal(root.right);
result.add(root.val);
result.addAll(left);
result.addAll(right);
return result;
}
```