LeetCode: Flatten Binary Tree To Linked List Solution

`.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}/**2 * Definition for a binary tree node.3 * function TreeNode(val, left, right) {4 *     this.val = (val===undefined ? 0 : val)5 *     this.left = (left===undefined ? null : left)6 *     this.right = (right===undefined ? null : right)7 * }8 */9/**10 * @param {TreeNode} root11 * @return {void} Do not return anything, modify root in-place instead.12 */13var flatten = function (root) {14  let iter = root15  while (iter) {16    if (iter.left) {17      let last = iter.left18      while (last.right) last = last.right19      last.right = iter.right20      iter.right = iter.left21      iter.left = null22    }23    iter = iter.right24  }25}`

leetcode

tree

Next Post

LeetCode: Valid Number

May 15, 2021

Search Posts