Skip to content

Commit df06aa0

Browse files
authored
Create Invert Binary Tree
1 parent aafdbbc commit df06aa0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Invert Binary Tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode invertTree(TreeNode root) {
18+
invert(root);
19+
return root;
20+
}
21+
public void invert(TreeNode root){
22+
if(root == null){
23+
return ;
24+
}
25+
TreeNode temp = root.right;
26+
root.right = root.left;
27+
root.left = temp;
28+
invert(root.left);
29+
invert(root.right);
30+
}
31+
}

0 commit comments

Comments
 (0)