Easy-to-understand recursive C# approach, above 99% efficiency


  • 0
    K
    public class Solution 
    {
        public TreeNode InvertTree(TreeNode root) 
        {
            if (root == null) return null;    
            // Swap
            TreeNode t = root.left;
            root.left = root.right;
            root.right = t;        
            // Traverse children
            InvertTree(root.left);
            InvertTree(root.right);
            return root;        
        }
    }
    

Log in to reply
 

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