-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlattenBinaryTree2LinkedList_114.java
More file actions
117 lines (100 loc) · 2.64 KB
/
FlattenBinaryTree2LinkedList_114.java
File metadata and controls
117 lines (100 loc) · 2.64 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
/**
* ----------------------------------------------------------------------------
Flatten Binary Tree to Linked List
- Given a binary tree, flatten it to a linked list in-place.
For example:
- Given
1
/ \
2 5
/ \ \
3 4 6
- The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
* ----------------------------------------------------------------------------
*/
/**
* Related: solutions 2 Convert BST to Sorted DoubleLinked List
* Tags: Binary Tree, Linked List
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/**
* - This is similar to Convert BST to Sorted DoubleLinked List
* - Instead it is a pre-order traversal convert
* - Notice that this is a single linked list, use right pointer only,
* and the left pointer is set to null
* - pair (head, tail), head = root, we only need to return the tail
* - O(n) time, and O(logn) space
*/
public class Solution {
public void flatten(TreeNode root) {
flat(root);
}
// input the start (root), return end of the link
public TreeNode flat(TreeNode root) {
if (root == null) return null;
if (root.left == null && root.right == null) return root;
// TreeNode right = flat(root.right);
TreeNode left = flat(root.left);
TreeNode right = flat(root.right);
TreeNode tail = null;
if (left == null) {
tail = right;
} else if (right == null) {
root.right = root.left;
root.left = null;
tail = left;
} else {
// neither is null
left.right = root.right;
root.right = root.left;
root.left = null;
tail = right;
}
return tail;
}
}
//------------------------------------------------------------------------------
/**
* Iterative Method - use a stack
* - It is not a typical Preorder Traversal, because we need to set
* root.right = root.left, so we need some way to store the right node info
* - So we use the most original pre-order iterative
*/
public class Solution {
public void flatten(TreeNode root) {
// XXXX do not forget
if (root == null) return;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode curr = stack.pop();
if (curr.right != null)
stack.push(curr.right);
if (curr.left != null)
stack.push(curr.left);
if (!stack.isEmpty()) {
curr.right = stack.peek();
}
curr.left = null;
}
}
}