-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1660-correct-a-binary-tree.js
More file actions
63 lines (57 loc) · 1.77 KB
/
1660-correct-a-binary-tree.js
File metadata and controls
63 lines (57 loc) · 1.77 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
58
59
60
61
62
63
/**
* 1660. Correct a Binary Tree
* https://leetcode.com/problems/correct-a-binary-tree/
* Difficulty: Medium
*
* You have a binary tree with a small defect. There is exactly one invalid node where its right
* child incorrectly points to another node at the same depth but to the invalid node's right.
*
* Given the root of the binary tree with this defect, root, return the root of the binary tree
* after removing this invalid node and every node underneath it (minus the node it incorrectly
* points to).
*
* Custom testing:
* The test input is read as 3 lines:
* - TreeNode root
* - int fromNode (not available to correctBinaryTree)
* - int toNode (not available to correctBinaryTree)
*
* After the binary tree rooted at root is parsed, the TreeNode with value of fromNode will have
* its right child pointer pointing to the TreeNode with a value of toNode. Then, root is passed
* to correctBinaryTree.
*/
/**
* 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
* @param {number} from
* @param {number} to
* @return {TreeNode}
*/
var correctBinaryTree = function(root) {
const set = new Set();
return traverse(root, null, false);
function traverse(node, parent, isLeft) {
if (!node) return null;
if (node.right && set.has(node.right)) {
if (parent) {
if (isLeft) {
parent.left = null;
} else {
parent.right = null;
}
}
return null;
}
set.add(node);
node.right = traverse(node.right, node, false);
node.left = traverse(node.left, node, true);
return node;
}
};