forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0114-flatten-binary-tree-to-linked-list.js
39 lines (36 loc) · 1.14 KB
/
0114-flatten-binary-tree-to-linked-list.js
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
/**
* 114. Flatten Binary Tree to Linked List
* https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
* Difficulty: Medium
*
* Given the root of a binary tree, flatten the tree into a "linked list":
* - The "linked list" should use the same TreeNode class where the right child pointer points
* to the next node in the list and the left child pointer is always null.
* - The "linked list" should be in the same order as a pre-order traversal of the binary tree.
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*/
var flatten = function(root) {
if (root === null) return;
if (root.left) {
let previous = root.left;
while (previous.right) {
previous = previous.right;
}
const rightNode = root.right;
root.right = root.left;
previous.right = rightNode;
root.left = null;
}
flatten(root.right);
};