File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You canโt perform that action at this time.
0 commit comments