C# Solution


  • 0
        public void Flatten(TreeNode root) {
            if (root == null)
                return;
            
            TreeNode tempNode = null,
                     lastNode = null;
            
            Flatten(root.left);
            Flatten(root.right);
            
            if (root.left != null)
            {
                tempNode = root.right;
                root.right = root.left;
                root.left = null;
                lastNode = root.right;
                
                while (lastNode.right != null)
                    lastNode = lastNode.right;
                    
                lastNode.right = tempNode;
            }
        }

Log in to reply
 

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