-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathFlattenBinaryTreeToLinkedList114.java
148 lines (126 loc) · 3.63 KB
/
FlattenBinaryTreeToLinkedList114.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
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
/**
* 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
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class FlattenBinaryTreeToLinkedList114 {
public void flatten(TreeNode root) {
flattenNode(root);
}
private TreeNode flattenNode(TreeNode root) {
if (root == null) return null;
TreeNode left = root.left;
TreeNode right = root.right;
root.left = null;
if (left == null && right == null) {
return root;
} else if (left == null && right != null) {
TreeNode rightEnd = flattenNode(right);
return rightEnd;
} else if (left != null && right == null) {
TreeNode leftEnd = flattenNode(left);
root.right = left;
return leftEnd;
} else {
TreeNode leftEnd = flattenNode(left);
TreeNode rightEnd = flattenNode(right);
leftEnd.right = right;
root.right = left;
return rightEnd;
}
}
public void flatten2(TreeNode root) {
helper(root);
}
private TreeNode helper(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) return root;
TreeNode leftLast = helper(root.left);
if (leftLast != null) {
leftLast.right = root.right;
root.right = root.left;
root.left = null;
}
return helper(root.right);
}
/**
* https://leetcode.com/problems/flatten-binary-tree-to-linked-list/discuss/37010/Share-my-simple-NON-recursive-solution-O(1)-space-complexity!
*/
public void flatten3(TreeNode root) {
TreeNode cur = root;
while (cur != null) {
if (cur.left != null) {
TreeNode last = cur.left;
while (last.right != null) last = last.right;
last.right = cur.right;
cur.right = cur.left;
cur.left = null;
}
cur = cur.right;
}
}
/**
* https://leetcode.com/problems/flatten-binary-tree-to-linked-list/discuss/36977/My-short-post-order-traversal-Java-solution-for-share
*/
public void flatten4(TreeNode root) {
flatten(root,null);
}
private TreeNode flatten(TreeNode root, TreeNode pre) {
if(root==null) return pre;
pre=flatten(root.right,pre);
pre=flatten(root.left,pre);
root.right=pre;
root.left=null;
pre=root;
return pre;
}
public static void main(String[] args) {
FlattenBinaryTreeToLinkedList114 fbt2ll = new FlattenBinaryTreeToLinkedList114();
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
TreeNode n6 = new TreeNode(6);
n1.left = n2;
n1.right = n5;
n2.left = n3;
n2.right = n4;
n5.right = n6;
fbt2ll.flatten(n1);
System.out.println("Out!");
System.out.println(n1.val);
System.out.println(n1.right.val);
System.out.println(n1.right.right.val);
System.out.println(n1.right.right.right.val);
System.out.println(n1.right.right.right.right.val);
System.out.println(n1.right.right.right.right.right.val);
}
}