forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeUpsideDown.java
207 lines (180 loc) · 5.5 KB
/
BinaryTreeUpsideDown.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.leetcode.trees;
import java.util.Stack;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* Level: Medium
* Problem Link: https://leetcode.com/problems/binary-tree-upside-down/
* Problem Description:
* Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the
* same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into
* left leaf nodes. Return the new root.
*
* Example:
* Input: [1,2,3,4,5]
*
* 1
* / \
* 2 3
* / \
* 4 5
*
* Output: return the root of the binary tree [4,5,2,#,#,3,1]
*
* 4
* / \
* 5 2
* / \
* 3 1
*
* Clarification:
* Confused what [4,5,2,#,#,3,1] means? Read more below on how binary tree is serialized on OJ. The serialization of
* a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
*
* Here's an example:
*
* 1
* / \
* 2 3
* /
* 4
* \
* 5
*
* The above binary tree is serialized as [1,2,3,#,#,4,#,#,5].
*
* @author rampatra
* @since 2019-08-04
*/
public class BinaryTreeUpsideDown {
/**
* The solution is simple, every node (except the root) on the left of the tree would have its parent's right child
* as it's left child and parent as its right child. That's all you have to do to flip the tree upside down.
*
* Time Complexity: O(h)
* Space Complexity: O(h)
* where,
* h = height of the tree
*
* Runtime: <a href="https://leetcode.com/submissions/detail/248816514/">1 ms</a>.
*
* @param root
* @return
*/
public static TreeNode upsideDownBinaryTreeUsingStack(TreeNode root) {
if (root == null) return null;
TreeNode curr = root;
TreeNode currParent;
TreeNode newRoot = null;
// using stack to keep track of the parent node
Stack<TreeNode> stack = new Stack<>();
while (curr != null) {
stack.add(curr);
curr = curr.left;
}
while (!stack.empty()) {
curr = stack.pop();
currParent = stack.empty() ? null : stack.peek();
if (newRoot == null) newRoot = curr;
if (currParent != null) {
curr.left = currParent.right;
curr.right = currParent;
} else {
curr.left = null;
curr.right = null;
}
}
return newRoot;
}
/**
* The solution is simple, every node (except the root) on the left of the tree would have its parent's right child
* as it's left child and parent as its right child. That's all you have to do to flip the tree upside down.
*
* Time Complexity: O(h)
* Space Complexity: O(h)
* where,
* h = height of the tree
*
* Runtime: <a href="https://leetcode.com/submissions/detail/248821826/">0 ms</a>.
*
* @param node
* @return
*/
public static TreeNode upsideDownBinaryTree(TreeNode node) {
if (node == null || node.left == null) return node;
// go to the last node on the extreme left branch
TreeNode newRoot = upsideDownBinaryTree(node.left);
// do the node changes as you backtrack
node.left.left = node.right;
node.left.right = node;
// clean up
node.left = null;
node.right = null;
return newRoot;
}
public static void main(String[] args) {
/*
Binary Tree
1
/ \
2 3
/ \
4 5
*/
TreeNode tree = new TreeNode(1);
tree.left = new TreeNode(2);
tree.right = new TreeNode(3);
tree.left.left = new TreeNode(4);
tree.left.right = new TreeNode(5);
/*
Upside Down Binary Tree
4
/ \
5 2
/ \
3 1
*/
TreeNode upsideDownTree = upsideDownBinaryTreeUsingStack(tree);
assertEquals(4, upsideDownTree.val);
assertEquals(5, upsideDownTree.left.val);
assertEquals(2, upsideDownTree.right.val);
assertEquals(1, upsideDownTree.right.right.val);
assertEquals(3, upsideDownTree.right.left.val);
assertNull(upsideDownTree.right.right.left);
assertNull(upsideDownTree.right.right.right);
/******************************
*
* Test for the recursive method
*
******************************/
/*
Binary Tree
1
/ \
2 3
/ \
4 5
*/
tree = new TreeNode(1);
tree.left = new TreeNode(2);
tree.right = new TreeNode(3);
tree.left.left = new TreeNode(4);
tree.left.right = new TreeNode(5);
/*
Upside Down Binary Tree
4
/ \
5 2
/ \
3 1
*/
upsideDownTree = upsideDownBinaryTree(tree);
assertEquals(4, upsideDownTree.val);
assertEquals(5, upsideDownTree.left.val);
assertEquals(2, upsideDownTree.right.val);
assertEquals(1, upsideDownTree.right.right.val);
assertEquals(3, upsideDownTree.right.left.val);
assertNull(upsideDownTree.right.right.right);
assertNull(upsideDownTree.right.right.left);
}
}