Javascript solution using recursion


  • 0
    S
    /**
     * Definition for a binary tree node.
     * function TreeNode(val) {
     *     this.val = val;
     *     this.left = this.right = null;
     * }
     */
    /**
     * @param {TreeNode} root
     * @return {void} Do not return anything, modify root in-place instead.
     */
    var flatten = function(root) {
      if (!root) { return; }
      
      let left = root.left;
      let right = root.right;
      
      flatten(left);
      flatten(right);
      
      root.left = null;
      root.right = left;
      let cur = root;
      while (cur.right !== null) { cur = cur.right; }
      cur.right = right;
    };
    

Log in to reply
 

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