C# Solution O(n)


  • 0
    K
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left;
     *     public TreeNode right;
     *     public TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public void Flatten(TreeNode root) {
            Helper(root, null);
        }
        
        private TreeNode Helper(TreeNode node, TreeNode prev){
            if(node == null){
                return prev;
            }
            
            TreeNode right = node.right;
            if(prev != null){
                prev.left = null;
                prev.right = node;
            }
            prev = node;
            prev = Helper(node.left, prev);
            return Helper(right, prev);
        }
    }```

Log in to reply
 

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