forked from cherryljr/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Tree Preorder Traversal.java
More file actions
184 lines (166 loc) · 5 KB
/
Binary Tree Preorder Traversal.java
File metadata and controls
184 lines (166 loc) · 5 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
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
173
174
175
176
177
178
179
180
181
182
183
184
/*
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
*/
/**
* Approach 1: Recursion
* If we want to implement the Preorder Traversal.
* We just need to print/store the curr.node
* when we visited the curr node at the first time.
*
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
/**
* 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: A Tree
* @return: Preorder in ArrayList which contains node values.
*/
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> rst = new ArrayList<>();
preorder(rst, root);
return rst;
}
private void preorder(List<Integer> rst, TreeNode node) {
if (node != null) {
// Store the node when we visited it firstly
rst.add(node.val);
// inorder left subtree
preorder(rst, node.left);
// inorder right subtree
preorder(rst, node.right);
}
}
}
/**
* Approach 2: Using Stack
* 1. Check if root is null
* 2. use a container to save results
* store current node
* put right on stack
* put left on stack
* Note:
* In next run, the ‘left’ will be on top of stack, and will be taken first.
* So the order becomes: parent -> left -> right
*
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
public class Solution {
/**
* @param root: A Tree
* @return: Preorder in ArrayList which contains node values.
*/
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> rst = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.empty()) {
TreeNode node = stack.pop();
rst.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return rst;
}
}
/**
* Approach 3: Morris Traversal
* Reference:
* https://github.com/cherryljr/LintCode/blob/master/Morris%20Traversal%20Template.java
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
public class Solution {
/**
* @param root: A Tree
* @return: Preorder in ArrayList which contains node values.
*/
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
TreeNode curr = root;
TreeNode rightMost = null;
List<Integer> rst = new ArrayList<>();
while (curr != null) {
if (curr.left == null) {
/*
* If there is no left subtree, then we can visit this node and
* continue traversing right.
*/
// Store the curr.val when we visit the node firstly
rst.add(curr.val);
// move to next right node
curr = curr.right;
} else {
// if curr node has a left subtree
// then get rightmost node of left subtree
rightMost = getRightMostNode(curr);
if (rightMost.right == null) {
/*
* If the rightMost node's right subtree is null, then we have never been here before.
* (the first time that we visit the curr node)
* the current node should be the right child of the rightMost node.
*/
// Store the curr.val when we visit the node firstly
rst.add(curr.val);
rightMost.right = curr;
curr = curr.left;
} else {
/*
* If there is a right subtree, it is a link that we created on a previous pass,
* (the second time that we visit the curr node)
* so we should unlink it and visit this node to avoid infinite loops
*/
rightMost.right = null;
curr = curr.right;
}
}
}
return rst;
}
// Get the node with the right most position of left subtree of curr.
// Attention: node.right != curr, cuz the node.right may be linked to curr node before.
public TreeNode getRightMostNode(TreeNode curr) {
TreeNode node = curr.left;
while (node.right != null && node.right != curr) {
node = node.right;
}
return node;
}
}