forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwogha95.js
More file actions
33 lines (32 loc) · 761 Bytes
/
wogha95.js
File metadata and controls
33 lines (32 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 양쪽 자식 노드 주소를 교환하고 dfs로 순회합니다.
*
* TC: O(N)
* 모든 트리를 순회합니다.
*
* SC: O(N)
* 최악의 경우 (한쪽으로 치우친 트리) N만큼 CallStack이 생깁니다.
*
* N: tree의 모든 node 수
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function (root) {
if (!root) {
return root;
}
[root.left, root.right] = [root.right, root.left];
invertTree(root.left);
invertTree(root.right);
return root;
};