Skip to content

Commit a3fc067

Browse files
committed
invert-binary-tree solution
1 parent 024f8d3 commit a3fc067

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
* @return {TreeNode}
12+
*/
13+
var invertTree = function (root) {
14+
const dfs = (node) => {
15+
if (!node) {
16+
return null;
17+
}
18+
19+
const temp = node.left;
20+
node.left = node.right;
21+
node.right = temp;
22+
23+
dfs(node.left);
24+
dfs(node.right);
25+
}
26+
27+
dfs(root);
28+
29+
return root;
30+
};
31+
32+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n) ๊นŠ์ด์šฐ์„ ํƒ์ƒ‰์œผ๋กœ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฏ€๋กœ

โ€Žmaximum-product-subarray/jdy8739.jsโ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ var maxProduct = function (nums) {
2020
return answer;
2121
};
2222

23-
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n) * O(8) -> nums์˜ ๊ธธ์ด ๋งŒํผ์„ for ๋ฌธ์œผ๋กœ ์ˆœํ™˜ํ•˜๋ฉด์„œ Mathํด๋ž˜์Šค์˜ max, min๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ(์ธ์ž๊ฐ€ 3๊ฐœ, 3๊ฐœ, 2๊ฐœ ์ด๋ฏ€๋กœ ์ด 8ํšŒ ์ˆœํšŒ)
23+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(n) -> nums์˜ ๊ธธ์ด ๋งŒํผ์„ for ๋ฌธ ์ˆœํ™˜ํ•˜๊ธฐ๋•Œ๋ฌธ์—
2424
// ๊ณต๊ฐ„๋ณต์žก๋„ O(1) -> ํŒŒ๋ผ๋ฏธํ„ฐ nums์— ๋Œ€ํ•ด ์˜๋ฏธ์žˆ๋Š” ๊ณต๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๋Š” ๋ณ€์ˆ˜ํ• ๋‹น์ด ์—†์Œ
25+

0 commit comments

Comments
ย (0)