Given the root
of a binary tree, invert the tree, and return its root.
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) return null;
invertTree(root.left);
invertTree(root.right);
reverseChild(root);
return root;
}
public void reverseChild(TreeNode root){
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
}
}
Hints:
1: if(root == null) return null; It is important to focus on the type of retrun, if the key word is "void", we just use "return", or if it is "String or TreeNode", we should return "null"
2: Understand the theory of recursion, the logic and the code of inverting Binary Tree.