C# Simple recursive solution. No space


  • 0
    J
    public TreeNode InvertTree(TreeNode root) 
        {
            if (root == null) return root;
            Helper(root);
            return root;
        }
        
        private void Helper(TreeNode root)
        {
            if (root == null) return;
            
            // Invert the children of this node
            var temp = root.left;
            root.left = root.right;
            root.right = temp;
            
            // Call function on children
            Helper(root.left);
            Helper(root.right);
        }
    

Log in to reply
 

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