![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
递归
Candy_Rainbow_
这个作者很懒,什么都没留下…
展开
-
JS 力扣刷题 114. 二叉树展开为链表
var flatten = function(root) { //建立先序节点连表 const nodelist = []; function dg(t){ if(!t)return; nodelist.push(t); dg(t.left); dg(t.right); } dg(root); //对连表遍历,操作 for(let i = 1; i < node.原创 2022-04-28 10:05:50 · 565 阅读 · 0 评论 -
JS 力扣刷题112. 路径总和
var hasPathSum = function(root, targetSum) { if(!root)return false; if(!root.left && !root.right && root.val == targetSum)return true; return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - roo.原创 2022-04-27 15:44:44 · 277 阅读 · 0 评论 -
JS力扣刷题 110. 平衡二叉树
var isBalanced = function(root) { function deep(root){//求深度 if(!root)return 0; return Math.max(deep(root.left), deep(root.right)) + 1; } function dg(root){//递归 if(!root)return true; return Math.abs(deep(root.lef.原创 2022-04-27 09:55:50 · 662 阅读 · 0 评论 -
JS 力扣刷题 109. 有序链表转换二叉搜索树
思路: 参考上一条,先将链表变成数组。var sortedListToBST = function(head) { const nums = []; while(head){ nums.push(head.val); head = head.next; } const dg = (nums) => { if(!nums.length)return null; let mid = (num...原创 2022-04-26 16:35:07 · 422 阅读 · 0 评论 -
JS力扣刷题 108. 将有序数组转换为二叉搜索树
思路:var sortedArrayToBST = function(nums) { const dg = (nums) =>{ if(!nums.length)return null; let mid = (nums.length - 1) >> 1; let root = new TreeNode(nums[mid]); root.left = dg(nums.slice(0, mid));原创 2022-04-26 16:12:01 · 399 阅读 · 0 评论 -
JS 力扣刷题 106. 从中序与后序遍历序列构造二叉树
var buildTree = function(inorder, postorder){ if(!inorder.length)return null; let root = new TreeNode(postorder[postorder.length - 1]); let m = inorder.findIndex(item => item === postorder[postorder.length - 1]); root.left = buildTree(i.原创 2022-04-26 16:10:10 · 187 阅读 · 0 评论 -
JS 力扣刷题 105. 从前序与中序遍历序列构造二叉树
var buildTree = function(preorder, inorder) { let l1 = 0, l2 = 0, r1 = preorder.length - 1, r2 = inorder.length - 1; const dg = (l1, l2, r1, r2) => { if(l1 > r1)return null;//没有根节点 let root = new TreeNode(preorder[l1]); .原创 2022-04-24 22:13:34 · 348 阅读 · 0 评论 -
力扣刷题 101. 对称二叉树
var isSymmetric = function(root) { const isTrue = (left, right) => {//左右子树是不是镜像 if(left == null && right == null)return true;//走到了根节点 if(left == null || right == null)return false;//左右子树有一个不空 if(left.val != right.val.原创 2022-04-22 17:17:25 · 439 阅读 · 0 评论 -
JS力扣刷题 100. 相同的树
var isSameTree = function(p, q) { let flag = true; const compare = (p, q) => { if(p == null && q == null)return;//p q都为空时返回 if(p == null || q == null){//只有一个为空 false flag = false; return; .原创 2022-04-19 19:15:50 · 209 阅读 · 0 评论 -
JS力扣刷题 98. 验证二叉搜索树
var isValidBST = function(root) { //中序遍历排序二叉树必是有序的 let flag = true; let pre = -Infinity; const isBst = t =>{ if(t == null)return; isBst(t.left); if(t.val <= pre)flag = false; pre = t.val; isBst.原创 2022-04-19 13:29:35 · 806 阅读 · 0 评论 -
JS力扣刷题 94. 二叉树的中序遍历
var inorderTraversal = function(root) { let res = []; function inorderVisit(t){ if(t){ inorderVisit(t.left); res.push([t.val]); inorderVisit(t.right); } } inorderVisit(root); return r.原创 2022-04-17 23:17:59 · 431 阅读 · 0 评论 -
JS力扣刷题70. 爬楼梯
var climbStairs = function(n) { // 递归超时 // function f(n){ // if(n == 1 || n == 2)return n; // return climbStairs[n - 1] + climbStairs[n - 2]; // } let res = new Array(n+1); res[1] = 1; res[2] = 2; for(let i = 3;.原创 2022-04-12 12:57:56 · 174 阅读 · 0 评论