-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Upload invert-binary-tree(typescript)
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Source: https://leetcode.com/problems/invert-binary-tree/ | ||
* 풀이방법: 재귀를 이용하여 트리를 뒤집음 | ||
* | ||
* 시간복잡도: O(n) - n은 트리의 노드 수 | ||
* 공간복잡도: O(n) - 재귀 호출에 따른 스택 메모리 | ||
* | ||
* 다른 풀이방법 | ||
* - BFS를 이용하여 풀이 | ||
* - 스택을 이용하여 풀이 | ||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* class TreeNode { | ||
* val: number | ||
* left: TreeNode | null | ||
* right: TreeNode | null | ||
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
* } | ||
*/ | ||
|
||
function invertTree(root: TreeNode | null): TreeNode | null { | ||
if (!root) return null; | ||
|
||
const result = new TreeNode( | ||
root.val, | ||
invertTree(root.right), | ||
invertTree(root.left) | ||
); | ||
return result; | ||
} |