Skip to content

Commit 22b7993

Browse files
committed
feat: add another js solution to lc problem: no.0226
1 parent 5782cfd commit 22b7993

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

solution/0200-0299/0226.Invert Binary Tree/Solution.js

+20
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,23 @@ var invertTree = function (root) {
1616
invertTree(root.right);
1717
return root;
1818
};
19+
20+
//non-recursion
21+
var invertTree = function (root) {
22+
if (!root) {
23+
return null;
24+
}
25+
let q = [];
26+
q.push(root);
27+
while (q.length) {
28+
let cur = q.pop();
29+
[cur.left, cur.right] = [cur.right, cur.left];
30+
if (cur.left) {
31+
q.push(cur.left);
32+
}
33+
if (cur.right) {
34+
q.push(cur.right);
35+
}
36+
}
37+
return root;
38+
}

0 commit comments

Comments
 (0)