File tree 1 file changed +46
-0
lines changed
1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
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 = function ( root ) {
16
+ let q = [ root ]
17
+ let target
18
+ while ( q . length ) {
19
+ const size = q . length
20
+ const next = new Set ( )
21
+ const row = new Set ( )
22
+ for ( let i = 0 ; i < size ; i ++ ) {
23
+ const cur = q . shift ( )
24
+ row . add ( cur )
25
+ if ( cur . left ) next . add ( cur . left )
26
+ if ( cur . right ) next . add ( cur . right )
27
+ }
28
+ for ( let e of next ) {
29
+ if ( next . has ( e . right ) ) {
30
+ target = e
31
+ for ( let el of row ) {
32
+ if ( el . left && el . left === target ) {
33
+ el . left = null
34
+ return root
35
+ }
36
+ if ( el . right && el . right === target ) {
37
+ el . right = null
38
+ return root
39
+ }
40
+ }
41
+ }
42
+ }
43
+ q = Array . from ( next )
44
+ }
45
+ return root
46
+ } ;
You can’t perform that action at this time.
0 commit comments