Invert Binary Tree
Problem Link
Invert a Binary Tree (opens in a new tab)
Problem Statement
- Here's a tree, and we need to reverse/invert the elements of the tree on each level
- Check how in each level, the nodes are changed across subtrees.
Algorithm
A main part about trees is recursions. We need to swap the elements of each node on each level.
- Base case: if the root is null, return null.
- Do a recursion for the left of root and right of root.
- Swap the left and right of root using a temporary variable.
- Return the root
Code
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root==null){
return null;
}
invertTree(root.left);
invertTree(root.right);
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
return root;
}
}