Skip to content

Commit cff5b84

Browse files
committed
Runtime: 59 ms (Top 52.37%) | Memory: 15.1 MB (Top 89.03%)
1 parent 90e3b19 commit cff5b84

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
# Runtime: 59 ms (Top 52.37%) | Memory: 15.1 MB (Top 89.03%)
12
#Call the right of the tree node till the node root left and right is not None
2-
#After reaching the bottom of the tree make the root.right = prev and
3+
#After reaching the bottom of the tree make the root.right = prev and
34
#root.left = None and then prev = None
45
#Initially prev will point to None but this is used to point the previously visited root node
56
#Prev pointer helps us to change the values from left to right
@@ -8,20 +9,20 @@ def flatten(self, root: Optional[TreeNode]) -> None:
89
"""
910
Do not return anything, modify root in-place instead.
1011
"""
11-
prev = None #You can also define that variable inside the init function using self keyword
12+
prev = None #You can also define that variable inside the init function using self keyword
1213
def dfs(root):
1314
nonlocal prev
14-
15+
1516
if not root:
1617
return
17-
18+
1819
dfs(root.right)
1920
dfs(root.left)
20-
21+
2122
root.right = prev
2223
root.left = None
2324
prev = root
24-
25+
2526
dfs(root)
2627
# If the above solution is hard to understand than one can do level order traversal
27-
#Using Stack DS but this will increase the space complexity to O(N).
28+
#Using Stack DS but this will increase the space complexity to O(N).

0 commit comments

Comments
 (0)