-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatten-binary-tree-to-linked-list.js
More file actions
57 lines (48 loc) · 1.53 KB
/
flatten-binary-tree-to-linked-list.js
File metadata and controls
57 lines (48 loc) · 1.53 KB
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
/**
* Problem: Flatten Binary Tree to Linked List
* Link: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
* Difficulty: Medium
*
* Flatten the tree to a "linked list" in-place using preorder traversal.
* Use the right pointer as next, left should be null.
*
* Example: [1,2,5,3,4,null,6] => [1,null,2,null,3,null,4,null,5,null,6]
*
* Time Complexity: O(n)
* Space Complexity: O(1) for Morris-like approach
*/
// JavaScript Solution - Morris-like O(1) space
function flatten(root) {
let current = root;
while (current) {
if (current.left) {
// Find the rightmost node of the left subtree
let rightmost = current.left;
while (rightmost.right) {
rightmost = rightmost.right;
}
// Connect rightmost to current's right subtree
rightmost.right = current.right;
// Move left subtree to right
current.right = current.left;
current.left = null;
}
current = current.right; // move to next node
}
}
module.exports = flatten;
/* Python Solution:
def flatten(root):
current = root
while current:
if current.left:
# Find rightmost node in left subtree
rightmost = current.left
while rightmost.right:
rightmost = rightmost.right
# Rewire: left subtree goes to right, old right goes after rightmost
rightmost.right = current.right
current.right = current.left
current.left = None
current = current.right # advance
*/