forked from algorithm024/algorithm024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvertTree_226.java
More file actions
43 lines (36 loc) · 895 Bytes
/
Copy pathInvertTree_226.java
File metadata and controls
43 lines (36 loc) · 895 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
36
37
38
39
40
41
42
43
public class InvertTree_226 {
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
/**
* 使用递归的方法求解
* @param root
* @return
*/
public TreeNode invertTree(TreeNode root) {
// 递归终止逻辑
if (root == null) {
return null;
}
// 当前层递归逻辑
TreeNode node = root.left;
root.left = root.right;
root.right = node;
// 下探到下一层
invertTree(root.left);
invertTree(root.right);
return root;
}
}