-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path144. Binary Tree Preorder Traversal (DFS +Stack) 20.3.7 Medium
More file actions
132 lines (121 loc) · 3.88 KB
/
144. Binary Tree Preorder Traversal (DFS +Stack) 20.3.7 Medium
File metadata and controls
132 lines (121 loc) · 3.88 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
Given a binary tree, return the preorder traversal of its nodes' values.
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,2,3]
Follow up: Recursive solution is trivial, could you do it iteratively?
Solution: ----------------------------------------------------------- DFS
----------------------------------------------------------- O(n) T, O(n) S
class Solution(object):
def preorderTraversal(self, root):
res = []
self.traver_help(res, root)
return res
"""
:type root: TreeNode
:rtype: List[int]
"""
def traver_help(self, res, root):
if not root is None:
res.append(root.val)
self.traver_help(res, root.left)
self.traver_help(res, root.right)
'''
Tomorrow is midterm,
So just refresh the resursion solution for DFS traversal,
the iterative solution will be followed up later on.
'''
Solution: ----------------------------------------------------------- Stack
----------------------------------------------------------- O(n) T, O(n) S
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if not root:
return None
stack, res = [root], []
while stack:
pop_element = stack.pop()
res.append(pop_element.val) # Append the node.val first -> preorder
# Make use of the First in Last out property for stack,
# So, we append node.right before append node.left
# In the next interation, node = node.left and continue
if pop_element.right: # IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# In both stack and queue question, everytime we want to append,
# we should test whether it is None, because None is meaningless and
# may bring "NoneType object has no attribute" error in next iteration
stack.append(pop_element.right)
if pop_element.left:
stack.append(pop_element.left)
return res
Java Version:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
dfs(root, res);
return res;
}
public void dfs(TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
res.add(root.val);
dfs(root.left, res);
dfs(root.right, res);
}
}
Java Version for solution no.2:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
Stack<TreeNode> s = new Stack<>();
s.add(root);
while (s.size() != 0) {
TreeNode popNode = s.pop();
res.add(popNode.val);
if (popNode.right != null) {
s.add(popNode.right);
}
if (popNode.left != null) {
s.add(popNode.left);
}
}
return res;
}
}