Flashcards
Mirror BST
Cards in this deck (1)
- 1Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1class Solution {
public TreeNode invertTree(TreeNode root) {
if(root ==null) {
return root;
}
root=invertOperation(root,root.left,root.right);
return root;
}
public TreeNode invertOperation(TreeNode root,TreeNode left,TreeNode right) {
if(root ==null) {
return root;
}
if(left!=null) {
invertOperation(left,left.left,left.right);
}
TreeNode temp=new TreeNode(0);
temp=root.left;//temp=1
root.left=root.right;//left=3
root.right=temp;//right=1
if(right!=null) {
invertOperation(right,right.left,right.right);
}
return root;
}
}
Information
- Description
- Flashcards on Mirror BST, created by Suhas S on 12/05/2018.
- Created by
- Suhas S
- Views
- 0
- Rating