forked from forging2012/JavaArithmetic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode226.java
More file actions
35 lines (26 loc) · 757 Bytes
/
LeetCode226.java
File metadata and controls
35 lines (26 loc) · 757 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
34
35
package LeetCode;
public class LeetCode226 {
/// 226. Invert Binary Tree
/// https://leetcode.com/problems/invert-binary-tree/description/
/// 时间复杂度: O(n), n为树中节点个数
/// 空间复杂度: O(h), h为树的高度
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public TreeNode invertTree(TreeNode root) {
if (root == null)
return null;
// 左边翻转,右边翻转
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
// 左右交换!
root.left = right;
root.right = left;
return root;
}
}