Easy to understand java recursion solution

  • 0

    The basic idea is that when access root, we will swap root.left and root.right, then we go to these two subtree and repeat this operation.

    public class Solution {
        public TreeNode invertTree(TreeNode root) {
            if(root == null) return null;
            TreeNode temp = root.left;
            root.left = root.right;
            root.right = temp;
            return root;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.