-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion226.java
More file actions
43 lines (40 loc) · 1.19 KB
/
Copy pathQuestion226.java
File metadata and controls
43 lines (40 loc) · 1.19 KB
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 Question226
{
public static TreeNode invertTree(TreeNode root)
{
/*
Pseudo Code
1. Swap .left with .right
2. Invert .left and .right
3. return root
*/
if(root == null)
return root;
if(root.left != null || root.right!=null)
{
TreeNode temp = root.left;
root.left = invertTree(root.right);
root.right = invertTree(temp);
}
return root;
}
public static void main(String[] args)
{
TreeNode root = new TreeNode(10,null,null);
TreeNode l1 = new TreeNode(1,null,null);
TreeNode r1 = new TreeNode(3,null,null);
TreeNode ll2 = new TreeNode(-2,null,null);
TreeNode rr2 = new TreeNode(20,null,null);
TreeNode last = new TreeNode(33,null,null);
TreeNode lastnew = new TreeNode(123,null,null);
root.left = l1;
root.right = r1;
l1.left = ll2;
r1.right = rr2;
rr2.right = last;
last.left = lastnew;
Trees.display(root);
TreeNode result = invertTree(root);
Trees.display(result);
}
}