从零开始Leetcode – Day 16
Flatten Binary Tree to Linked List
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
1 注意考虑起始条件, 和终止条件。
2 晚上做不了算法题, 白天做过的,到了晚上不能debug.
3 所有题目要重做一次, 才会产生效果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| /** * 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) {
var pre = []; pre.push(null); helper(root, pre); };
var helper = function(root, pre){ if(root ===null){ return; } var right = new TreeNode(); right = root.right; if(pre[0] !== null){ pre[0].left = null; pre[0].right = root; } pre.unshift(root); helper(root.left, pre); helper(right, pre); };
|