C# DFS Recursive Solution


  • 0
    M
    public class Solution {
        private TreeNode previous;
        public void Flatten(TreeNode root) {
            if(root!=null)
            {
                TreeNode left = root.left;
                TreeNode right = root.right;
                if(previous!=null)
                {
                    previous.right = root;
                    previous.left = null;
                }
                previous = root;
                Flatten(left);
                Flatten(right);
            }
        }
    }
    

Log in to reply
 

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