My simple Java solution with recursion


  • 0
    R
    public class Solution {
        public TreeNode invertTree(TreeNode root) {
          if(root==null) return null;
          
          if(root.left==null && root.right == null) return root;
          
          TreeNode lNode = root.left;
          TreeNode rNode = root.right;
          
          
             root.right = lNode;
             invertTree(root.right);
         
         
              root.left = rNode;
              invertTree(root.left);
          
          return root;
        }
    }

  • 0
    A
    This post is deleted!

Log in to reply
 

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