Skip to content

Commit 825b3ed

Browse files
committed
O(n) time and O(1) space.
1 parent 31ebf9c commit 825b3ed

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
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.
3+
4+
Example:
5+
6+
Input: [1,2,3,4,5]
7+
8+
1
9+
/ \
10+
2 3
11+
/ \
12+
4 5
13+
14+
Output: return the root of the binary tree [4,5,2,#,#,3,1]
15+
16+
4
17+
/ \
18+
5 2
19+
/ \
20+
3 1
21+
Clarification:
22+
23+
Confused what [4,5,2,#,#,3,1] means? Read more below on how binary tree is serialized on OJ.
24+
25+
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
26+
27+
Here's an example:
28+
29+
1
30+
/ \
31+
2 3
32+
/
33+
4
34+
\
35+
5
36+
The above binary tree is serialized as [1,2,3,#,#,4,#,#,5].
37+
"""

0 commit comments

Comments
 (0)