-
Notifications
You must be signed in to change notification settings - Fork 0
/
226.翻转二叉树.ts
52 lines (40 loc) · 1.15 KB
/
226.翻转二叉树.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* @lc app=leetcode.cn id=226 lang=typescript
*
* [226] 翻转二叉树
*/
// @lc code=start
/**
* 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;
// // 使用数组解构 反转 + 递归
// [root.left, root.right] = [invertTree(root.right), invertTree(root.left)]
// // 返回 递归后的 root
// return root
// };
function invertTree(root: TreeNode | null): TreeNode | null {
if (!root) return null
// 递归 + 转换节点
// 1. 保存 右子树
const temp = root.right
// 2. 右子树 = 左子树
root.right = invertTree(root.left)
// 3. 左子树 = 保存的右子树
root.left = invertTree(temp)
// 4. 返回 root
return root
};
// @lc code=end