Skip to content

Commit 4604a53

Browse files
authored
Update 1660-correct-a-binary-tree.js
1 parent 4f8655f commit 4604a53

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

1660-correct-a-binary-tree.js

+30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @param {number} from
12+
* @param {number} to
13+
* @return {TreeNode}
14+
*/
15+
const correctBinaryTree = (root, seen = new Set(), found = false) => {
16+
const go = (root) => {
17+
seen.add(root)
18+
if (root.right && seen.has(root.right)) {
19+
found = true
20+
return null
21+
}
22+
if (!found && root.right) root.right = go(root.right)
23+
if (!found && root.left) root.left = go(root.left)
24+
return root
25+
}
26+
return go(root)
27+
}
28+
29+
// another
30+
131
/**
232
* Definition for a binary tree node.
333
* function TreeNode(val, left, right) {

0 commit comments

Comments
 (0)