Skip to content

Commit 3d8dca3

Browse files
authored
Update 226-invert-binary-tree.js
1 parent 474e7c6 commit 3d8dca3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

226-invert-binary-tree.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,35 @@ const invertTree = function (root) {
1616
}
1717
return root
1818
}
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+
}

0 commit comments

Comments
 (0)