-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathFlattenBinaryTreeToLinkedList.java
116 lines (104 loc) · 2.95 KB
/
FlattenBinaryTreeToLinkedList.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
/*
* Flatten a binary tree to a fake "linked list"
* in pre-order traversal.
Here we use the right pointer in TreeNode as the
next pointer in ListNode.
Example
1
\
1 2
/ \ \
2 5 => 3
/ \ \ \
3 4 6 4
\
5
\
6
Note
Don't forget to mark the left child of each node
to null. Or you will get Time Limit Exceeded or
Memory Limit Exceeded.
Challenge
Do it in-place without any extra memory.
*/
/**
* 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 FlattenBinaryTreeToLinkedList {
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
public void flatten(TreeNode root) {
while (root != null) {
if (root.left != null) {
TreeNode cur = root.left;
while (cur.right != null) {
cur = cur.right;
}
cur.right = root.right;
root.right = root.left;
root.left = null;
}
root = root.right;
}
}
/*******************************************************************/
public void flatten(TreeNode root) {
if (root == null) {
return;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
node.left = null;
if (!stack.isEmpty()) {
node.right = stack.peek();
}
}
}
/*******************************************************************/
public void flatten(TreeNode root) {
flatten(root, null);
}
public TreeNode flatten(TreeNode root, TreeNode result) {
if (root == null) {
return result;
}
result = flatten(root.right, result);
root.right = flatten(root.left, result);
root.left = null;
return root;
}
/*******************************************************************/
public void flatten(TreeNode root) {
if (root == null) {
return;
}
flatten(root.left);
flatten(root.right);
TreeNode right = root.right;
root.right = root.left;
root.left = null;
while (root.right != null) {
root = root.right;
}
root.right = right;
}
}