Skip to content

Commit

Permalink
feat: Upload invert-binary-tree(typescript)
Browse files Browse the repository at this point in the history
  • Loading branch information
mike2ox committed Feb 10, 2025
1 parent 356e622 commit 6d6de99
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions invert-binary-tree/mike2ox.ts
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;
}

0 comments on commit 6d6de99

Please sign in to comment.