Skip to content

Commit 486a6ef

Browse files
authored
Merge pull request #1016 from forest000014/main
[forest000014] Week 10
2 parents 26c70fa + 961790a commit 486a6ef

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
# Time Complexity: O(n)
3+
- ์ „์ฒด ๋…ธ๋“œ๋ฅผ 1๋ฒˆ์”ฉ ํƒ์ƒ‰
4+
# Space Complexity: O(n)
5+
- ์žฌ๊ท€ ํ˜ธ์ถœ์˜ ๊ฐ depth๋งˆ๋‹ค temp ๋…ธ๋“œ ํ•˜๋‚˜์”ฉ ์ƒ์„ฑ
6+
# Solution
7+
- ํ˜„์žฌ ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์ž์‹๊ณผ ์˜ค๋ฅธ์ชฝ ์ž์‹์„ ๊ฐ๊ฐ ์žฌ๊ท€ ํ˜ธ์ถœํ•˜์—ฌ ์ž์‹์˜ ์ž์‹ ๋…ธ๋“œ๋“ค์„ ๋ฐ˜์ „์‹œํ‚จ๋’ค,
8+
- ์™ผ์ชฝ ์ž์‹๊ณผ ์˜ค๋ฅธ์ชฝ ์ž์‹์„ ๋ฐ˜์ „์‹œํ‚ต๋‹ˆ๋‹ค.
9+
- base condition์œผ๋กœ, ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ null์ธ ๊ฒฝ์šฐ null์„ early return ํ•ฉ๋‹ˆ๋‹ค.
10+
*/
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* public class TreeNode {
15+
* int val;
16+
* TreeNode left;
17+
* TreeNode right;
18+
* TreeNode() {}
19+
* TreeNode(int val) { this.val = val; }
20+
* TreeNode(int val, TreeNode left, TreeNode right) {
21+
* this.val = val;
22+
* this.left = left;
23+
* this.right = right;
24+
* }
25+
* }
26+
*/
27+
class Solution {
28+
public TreeNode invertTree(TreeNode root) {
29+
if (root == null) {
30+
return null;
31+
}
32+
33+
invertTree(root.left);
34+
invertTree(root.right);
35+
36+
TreeNode temp = root.left;
37+
root.left = root.right;
38+
root.right = temp;
39+
40+
return root;
41+
}
42+
}

0 commit comments

Comments
ย (0)