/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } *//** * @param {TreeNode} root * @return {void} Do not return anything, modify root in-place instead. */var flatten = function (root) { const list = [] const helper = (node) => { if (!node) return list.push(node) helper(node.left) helper(node.right) } helper(root) for(let i = 1; i < list.length; i++) { const pre = list[i - 1], cur = list[i] pre.right = cur pre.left = null }};