Skip to content

Commit 5edf061

Browse files
committed
Create README - LeetHub
1 parent 31d5998 commit 5edf061

File tree

1 file changed

+89
-0
lines changed
  • Flatten binary tree to linked list - GFG

1 file changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Flatten binary tree to linked list
2+
## Medium
3+
<div class="problems_problem_content__Xm_eO"><p><span style="font-size:18px">Given the root of a binary tree, flatten the tree into a "linked list":</span></p>
4+
5+
<ul>
6+
<li><span style="font-size:18px">The "linked list" should use the same Node class where the right child pointer points to the next&nbsp; &nbsp;node in the list and the left child pointer is always null.</span></li>
7+
<li><span style="font-size:18px">The "linked list" should be in the same order as a pre-order traversal of the binary tree.</span></li>
8+
</ul>
9+
10+
<p><strong><span style="font-size:18px">Example 1:</span></strong></p>
11+
12+
<pre><span style="font-size:18px"><strong>Input : </strong>
13+
1
14+
/ \
15+
2 5
16+
/ \ \
17+
3 4 6</span>
18+
<span style="font-size:18px"><strong>Output :</strong>
19+
1 2 3 4 5 6
20+
<strong>Explanation: </strong>
21+
After flattening, the tree looks
22+
like this
23+
1
24+
\
25+
2
26+
\
27+
3
28+
\
29+
4
30+
\
31+
5
32+
\
33+
6
34+
Here, left of each node points
35+
to NULL and right contains the
36+
next node in preorder.The inorder
37+
traversal of this flattened tree
38+
is 1 2 3 4 5 6.</span></pre>
39+
40+
<p><strong><span style="font-size:18px">Example 2:</span></strong></p>
41+
42+
<pre><span style="font-size:18px"><strong>Input :</strong>
43+
1
44+
/ \
45+
3 4
46+
/
47+
2
48+
\
49+
5
50+
<strong>Output :</strong>
51+
1 3 4 2 5
52+
<strong>Explanation : </strong>
53+
After flattening, the tree looks
54+
like this
55+
1
56+
\
57+
3
58+
\
59+
4
60+
\
61+
2
62+
\
63+
5
64+
Here, left of each node points
65+
to NULL and right contains the
66+
next node in preorder.The inorder
67+
traversal of this flattened tree
68+
is 1 3 4 2 5.</span></pre>
69+
70+
<div><strong><span style="font-size:18px">Your task:</span></strong></div>
71+
72+
<div><span style="font-size:18px">You don't have to read input or print anything. Your task is to complete the function flatten() which takes the root of the tree and flattens the tree into a linkes list without using any auxiliary space.</span></div>
73+
74+
<div><span style="font-size:18px">Note : The driver code&nbsp;prints the inorder traversal of the flattened binary tree.</span></div>
75+
76+
<div>&nbsp;</div>
77+
78+
<div><span style="font-size:18px"><strong>Expected Time Complexity: </strong>O(n)</span></div>
79+
80+
<div><span style="font-size:18px"><strong>Expected Auxiliary Space:</strong> O(1)</span></div>
81+
82+
<div>&nbsp;</div>
83+
84+
<div><strong><span style="font-size:18px">Constraints :</span></strong></div>
85+
86+
<div><span style="font-size:18px">1&lt;=n&lt;=10^5</span></div>
87+
88+
<div>&nbsp;</div>
89+
</div>

0 commit comments

Comments
 (0)