1+ package  com .leetcode .trees ;
2+ 
3+ import  java .util .Stack ;
4+ 
5+ import  static  org .junit .jupiter .api .Assertions .assertEquals ;
6+ import  static  org .junit .jupiter .api .Assertions .assertNull ;
7+ 
8+ /** 
9+  * Level: Medium 
10+  * Problem Link: https://leetcode.com/problems/binary-tree-upside-down/ 
11+  * Problem Description: 
12+  * Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the 
13+  * same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into 
14+  * left leaf nodes. Return the new root. 
15+  * 
16+  * Example: 
17+  * Input: [1,2,3,4,5] 
18+  * 
19+  *     1 
20+  *    / \ 
21+  *   2   3 
22+  *  / \ 
23+  * 4   5 
24+  * 
25+  * Output: return the root of the binary tree [4,5,2,#,#,3,1] 
26+  * 
27+  *    4 
28+  *   / \ 
29+  *  5   2 
30+  *     / \ 
31+  *    3   1 
32+  * 
33+  * Clarification: 
34+  * Confused what [4,5,2,#,#,3,1] means? Read more below on how binary tree is serialized on OJ. The serialization of 
35+  * a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below. 
36+  * 
37+  * Here's an example: 
38+  * 
39+  *    1 
40+  *   / \ 
41+  *  2   3 
42+  *     / 
43+  *    4 
44+  *     \ 
45+  *      5 
46+  * 
47+  * The above binary tree is serialized as [1,2,3,#,#,4,#,#,5]. 
48+  * 
49+  * @author rampatra 
50+  * @since 2019-08-04 
51+  */ 
52+ public  class  BinaryTreeUpsideDown  {
53+ 
54+     /** 
55+      * The solution is simple, every node (except the root) on the left of the tree would have its parent's right child 
56+      * as it's left child and parent as its right child. That's all you have to do to flip the tree upside down. 
57+      * 
58+      * Time Complexity: O(h) 
59+      * Space Complexity: O(h) 
60+      * where, 
61+      * h = height of the tree 
62+      * 
63+      * Runtime: <a href="https://leetcode.com/submissions/detail/248816514/">1 ms</a>. 
64+      * 
65+      * @param root 
66+      * @return 
67+      */ 
68+     public  static  TreeNode  upsideDownBinaryTreeUsingStack (TreeNode  root ) {
69+         if  (root  == null ) return  null ;
70+ 
71+         TreeNode  curr  = root ;
72+         TreeNode  currParent ;
73+         TreeNode  newRoot  = null ;
74+ 
75+         // using stack to keep track of the parent node 
76+         Stack <TreeNode > stack  = new  Stack <>();
77+ 
78+         while  (curr  != null ) {
79+             stack .add (curr );
80+             curr  = curr .left ;
81+         }
82+ 
83+         while  (!stack .empty ()) {
84+             curr  = stack .pop ();
85+             currParent  = stack .empty () ? null  : stack .peek ();
86+ 
87+             if  (newRoot  == null ) newRoot  = curr ;
88+ 
89+             if  (currParent  != null ) {
90+                 curr .left  = currParent .right ;
91+                 curr .right  = currParent ;
92+             } else  {
93+                 curr .left  = null ;
94+                 curr .right  = null ;
95+             }
96+         }
97+ 
98+         return  newRoot ;
99+     }
100+ 
101+     /** 
102+      * The solution is simple, every node (except the root) on the left of the tree would have its parent's right child 
103+      * as it's left child and parent as its right child. That's all you have to do to flip the tree upside down. 
104+      * 
105+      * Time Complexity: O(h) 
106+      * Space Complexity: O(h) 
107+      * where, 
108+      * h = height of the tree 
109+      * 
110+      * Runtime: <a href="https://leetcode.com/submissions/detail/248821826/">0 ms</a>. 
111+      * 
112+      * @param node 
113+      * @return 
114+      */ 
115+     public  static  TreeNode  upsideDownBinaryTree (TreeNode  node ) {
116+         if  (node  == null  || node .left  == null ) return  node ;
117+ 
118+         // go to the last node on the extreme left branch 
119+         TreeNode  newRoot  = upsideDownBinaryTree (node .left );
120+ 
121+         // do the node changes as you backtrack 
122+         node .left .left  = node .right ;
123+         node .left .right  = node ;
124+ 
125+         // clean up 
126+         node .left  = null ;
127+         node .right  = null ;
128+ 
129+         return  newRoot ;
130+     }
131+ 
132+     public  static  void  main (String [] args ) {
133+         /* 
134+             Binary Tree 
135+ 
136+                 1 
137+                / \ 
138+               2   3 
139+              / \ 
140+             4   5 
141+          */ 
142+         TreeNode  tree  = new  TreeNode (1 );
143+         tree .left  = new  TreeNode (2 );
144+         tree .right  = new  TreeNode (3 );
145+         tree .left .left  = new  TreeNode (4 );
146+         tree .left .right  = new  TreeNode (5 );
147+ 
148+         /* 
149+             Upside Down Binary Tree 
150+ 
151+                4 
152+               / \ 
153+              5   2 
154+                 / \ 
155+                3   1 
156+          */ 
157+         TreeNode  upsideDownTree  = upsideDownBinaryTreeUsingStack (tree );
158+         assertEquals (4 , upsideDownTree .val );
159+         assertEquals (5 , upsideDownTree .left .val );
160+         assertEquals (2 , upsideDownTree .right .val );
161+         assertEquals (1 , upsideDownTree .right .right .val );
162+         assertEquals (3 , upsideDownTree .right .left .val );
163+         assertNull (upsideDownTree .right .right .left );
164+         assertNull (upsideDownTree .right .right .right );
165+ 
166+ 
167+ 
168+         /****************************** 
169+          * 
170+          * Test for the recursive method 
171+          * 
172+          ******************************/ 
173+ 
174+         /* 
175+             Binary Tree 
176+ 
177+                 1 
178+                / \ 
179+               2   3 
180+              / \ 
181+             4   5 
182+          */ 
183+         tree  = new  TreeNode (1 );
184+         tree .left  = new  TreeNode (2 );
185+         tree .right  = new  TreeNode (3 );
186+         tree .left .left  = new  TreeNode (4 );
187+         tree .left .right  = new  TreeNode (5 );
188+ 
189+         /* 
190+             Upside Down Binary Tree 
191+ 
192+                4 
193+               / \ 
194+              5   2 
195+                 / \ 
196+                3   1 
197+          */ 
198+         upsideDownTree  = upsideDownBinaryTree (tree );
199+         assertEquals (4 , upsideDownTree .val );
200+         assertEquals (5 , upsideDownTree .left .val );
201+         assertEquals (2 , upsideDownTree .right .val );
202+         assertEquals (1 , upsideDownTree .right .right .val );
203+         assertEquals (3 , upsideDownTree .right .left .val );
204+         assertNull (upsideDownTree .right .right .right );
205+         assertNull (upsideDownTree .right .right .left );
206+     }
207+ }
0 commit comments