-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvertBinaryTree.java
More file actions
52 lines (42 loc) · 900 Bytes
/
Copy pathInvertBinaryTree.java
File metadata and controls
52 lines (42 loc) · 900 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
44
45
46
47
48
49
50
51
52
/*
Invert a binary tree.
Example:
Input:
4
/ \
2 7
/ \ / \
1 3 6 9
Output:
4
/ \
7 2
/ \ / \
9 6 3 1
https://leetcode.com/problems/invert-binary-tree/
*/
public class InvertBinaryTree {
public static void main(String[] args) {
String[] input = new String[]{"ate", "eat", "tea", "tan", "nat", "bat"};
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public TreeNode invertTree(TreeNode root) {
return inorder(root);
}
private TreeNode inorder(TreeNode root) {
if (root == null)
return null;
TreeNode leftNode = inorder(root.left);
TreeNode rightNode = inorder(root.right);
root.right = leftNode;
root.left = rightNode;
return root;
}
}