File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -16,3 +16,35 @@ const invertTree = function (root) {
16
16
}
17
17
return root
18
18
}
19
+
20
+ // another
21
+
22
+ /**
23
+ * Definition for a binary tree node.
24
+ * function TreeNode(val, left, right) {
25
+ * this.val = (val===undefined ? 0 : val)
26
+ * this.left = (left===undefined ? null : left)
27
+ * this.right = (right===undefined ? null : right)
28
+ * }
29
+ */
30
+ /**
31
+ * @param {TreeNode } root
32
+ * @return {TreeNode }
33
+ */
34
+ const invertTree = function ( root ) {
35
+ if ( ! root ) return root
36
+ let queue = [ root ]
37
+ while ( queue . length ) {
38
+ let node = queue . shift ( )
39
+ if ( node . left ) {
40
+ queue . push ( node . left )
41
+ }
42
+ if ( node . right ) {
43
+ queue . push ( node . right )
44
+ }
45
+ let left = node . left
46
+ node . left = node . right
47
+ node . right = left
48
+ }
49
+ return root
50
+ }
You can’t perform that action at this time.
0 commit comments