File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ์ด์ง ํธ๋ฆฌ๋ฅผ ๋ฐ์ ์ํค๋ ํจ์
3+ * @param {TreeNode | null } root - ๋ฐ์ ํ ์ด์ง ํธ๋ฆฌ์ ๋ฃจํธ ๋
ธ๋
4+ * @returns {TreeNode | null } - ๋ฐ์ ๋ ์ด์ง ํธ๋ฆฌ์ ๋ฃจํธ ๋
ธ๋
5+ *
6+ * ์๊ฐ ๋ณต์ก๋: O(n)
7+ * - ํธ๋ฆฌ์ ๋ชจ๋ ๋
ธ๋๋ฅผ ํ ๋ฒ์ฉ ๋ฐฉ๋ฌธํด์ผ ํ๋ฏ๋ก ์ ํ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง
8+ * ๊ณต๊ฐ ๋ณต์ก๋: O(h)
9+ * - ์ฌ๊ท ํธ์ถ์ ์ํด ์ต๋ ํธ๋ฆฌ์ ๋์ด(h)๋งํผ์ ํธ์ถ ์คํ์ด ํ์
10+ */
11+ function invertTree ( root : TreeNode | null ) : TreeNode | null {
12+ // ๋ฃจํธ๊ฐ null์ด๋ฉด null ๋ฐํ
13+ if ( ! root ) return null ;
14+
15+ // ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ๋ฅผ ์ฌ๊ท์ ์ผ๋ก ๋ฐ์
16+ const left = invertTree ( root . left ) ;
17+ const right = invertTree ( root . right ) ;
18+
19+ // ํ์ฌ ๋
ธ๋์ ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ๋ฅผ ๊ตํ
20+ root . left = right ;
21+ root . right = left ;
22+
23+ // ๋ฐ์ ๋ ๋ฃจํธ ๋
ธ๋๋ฅผ ๋ฐํ
24+ return root ;
25+ }
26+
You canโt perform that action at this time.
0 commit comments