-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
226.java
51 lines (51 loc) · 1.37 KB
/
226.java
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
__________________________________________________________________________________________________
sample 32996 kb submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return root;
}
invertTree(root.left);
invertTree(root.right);
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
}
__________________________________________________________________________________________________
sample 32972 kb submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
TreeNode res = helper(root);
return res;
}
TreeNode helper(TreeNode root){
if (root == null)
return null;
TreeNode left = root.left;
TreeNode right = root.right;
root.left = helper(right);
root.right = helper(left);
return root;
}
}
__________________________________________________________________________________________________