-
Notifications
You must be signed in to change notification settings - Fork 171
/
BinaryTreePreorderTraversal.java
110 lines (108 loc) · 3.42 KB
/
BinaryTreePreorderTraversal.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
/*
Author: King, [email protected]
Date: Dec 25, 2014
Problem: Binary Tree Preorder Traversal
Difficulty: Easy
Source: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/
Notes:
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,2,3].
Note: Recursive solution is trivial, could you do it iteratively?
Solution: 1. Iterative way (stack). Time: O(n), Space: O(n).
2. Recursive solution. Time: O(n), Space: O(n).
3. Threaded tree (Morris). Time: O(n), Space: O(1).
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> preorderTraversal_1(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if(root == null) return res;
res.add(root.val);
List<Integer> left = preorderTraversal(root.left);
List<Integer> right = preorderTraversal(root.right);
res.addAll(left);
res.addAll(right);
return res;
}
public void preorderTraversalRe(TreeNode root, List<Integer> res) {
if (root == null) return;
res.add(root.val);
preorderTraversalRe(root.left, res);
preorderTraversalRe(root.right, res);
}
public List<Integer> preorderTraversal_2(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if(root == null) return res;
preorderTraversalRe(root, res);
return res;
}
public List<Integer> preorderTraversal_3(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if(root == null) return res;
Stack<TreeNode> stk = new Stack<TreeNode>();
stk.push(root);
while (stk.isEmpty() == false) {
TreeNode cur = stk.pop();
res.add(cur.val);
if (cur.right != null) stk.push(cur.right);
if (cur.left != null) stk.push(cur.left);
}
return res;
}
public List<Integer> preorderTraversal_4(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if(root == null) return res;
Stack<TreeNode> stk = new Stack<TreeNode>();
TreeNode cur = root;
while (stk.isEmpty() == false || cur != null) {
if (cur != null) {
stk.push(cur);
res.add(cur.val);
cur = cur.left;
} else {
cur = stk.pop();
cur = cur.right;
}
}
return res;
}
public List<Integer> preorderTraversal_5(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if(root == null) return res;
TreeNode cur = root;
while (cur) {
if (cur.left == null) {
res.add(cur.val);
cur = cur.right;
} else {
TreeNode node = cur.left;
while (node.right != null && node.right != cur)
node = node.right;
if (node == null) {
node.right = cur;
res.add(cur.val);
cur = cur.left;
} else {
node.right = null;
cur = cur.right;
}
}
}
return res;
}
}