Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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‎

Lines changed: 20 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /