洛阳网站建设内容,上海做网站的公司是什么,企业信息管理系统er图,wordpress后台邮箱怎么设置513. 找树左下角的值
最左边的结点的特性
1.只能是叶子结点#xff0c;
2.必须考虑是最底层#xff0c;所以要考虑树的深度
3.同样的深度考虑左子树
考虑迭代法,层序遍历
递归优点难搞的
/*** Definition for a binary tree node.* function TreeNode(val, left, righ…513. 找树左下角的值
最左边的结点的特性
1.只能是叶子结点
2.必须考虑是最底层所以要考虑树的深度
3.同样的深度考虑左子树
考虑迭代法,层序遍历
递归优点难搞的
/*** Definition for a binary tree node.* function TreeNode(val, left, right) {* this.val (valundefined ? 0 : val)* this.left (leftundefined ? null : left)* this.right (rightundefined ? null : right)* }*/
/*** param {TreeNode} root* return {number}*///最左边的结点的特性//1.只能是叶子结点//2.必须考虑是最底层所以要考虑树的深度//3.同样的深度考虑左子树//考虑迭代法,层序遍历
var findBottomLeftValue function(root) {let q [root], res [];while(q.length 0){let len q.length;let curLevel [];for(let i 0; i len; i){let curNode q.shift();curLevel.push(curNode.val);if(curNode.left) q.push(curNode.left);if(curNode.right) q.push(curNode.right);}res.push(curLevel);}return res[res.length - 1][0];
};112. 路径总和
/*** Definition for a binary tree node.* function TreeNode(val, left, right) {* this.val (valundefined ? 0 : val)* this.left (leftundefined ? null : left)* this.right (rightundefined ? null : right)* }*/
/*** param {TreeNode} root* param {number} targetSum* return {boolean}*/
var hasPathSum function(root, targetSum) {if(!root) return false;let res [];dfs(root, 0, res);console.log(res:,res);console.log(res.indexOf(targetSum));return res.indexOf(targetSum) -1 ? false : true;
}function dfs(node, sum, res){//叶子结点if(!node.left !node.right){res.push(sum node.val);return;}if(node.left) dfs(node.left, sum node.val, res);if(node.right) dfs(node.right, sum node.val, res);
}113. 路径总和 II
/*** Definition for a binary tree node.* function TreeNode(val, left, right) {* this.val (valundefined ? 0 : val)* this.left (leftundefined ? null : left)* this.right (rightundefined ? null : right)* }*/
/*** param {TreeNode} root* param {number} targetSum* return {number[][]}*/
var pathSum function(root, targetSum) {if(!root) return [];let res [];dfs(root, 0, res, [], targetSum);return res;
};function dfs(node, sum, res, path, targetSum){path.push(node.val);sum node.val;//叶子结点if(!node.left !node.right){if(sum targetSum){res.push([...path]);//这里不能直接res.push(path)因为JS中数组是直接传引用的所以最后return的res中的那个数组就是被修改过的path数组这里用扩展运算符} return;}if(node.left){dfs(node.left, sum, res, path, targetSum);path.pop();} if(node.right){dfs(node.right, sum, res, path, targetSum);path.pop();}
}106. 从中序与后序遍历序列构造二叉树
能过但是会超内存之后在改进吧
/*** Definition for a binary tree node.* function TreeNode(val, left, right) {* this.val (valundefined ? 0 : val)* this.left (leftundefined ? null : left)* this.right (rightundefined ? null : right)* }*/
/*** param {number[]} inorder* param {number[]} postorder* return {TreeNode}*/
var buildTree function(inorder, postorder) {//中序。 左中右//后序。 左右中if(inorder.length 0) return null;let val postorder[postorder.length - 1];let root new TreeNode(val);let index inorder.indexOf(val);let leftInOrder inorder.slice(0, index);let rightInOrder inorder.slice(index 1);let index2 postorder.indexOf(leftInOrder[leftInOrder.length - 1]);let leftPostOrder postorder.slice(0, index2 1);let rightPostOeder postorder.slice(index2 1, postorder.length - 1);root.left buildTree(leftInOrder, leftPostOrder);root.right buildTree(rightInOrder, rightPostOeder);return root;
}; 文章转载自: http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.llmhq.cn.gov.cn.llmhq.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.gczzm.cn.gov.cn.gczzm.cn http://www.morning.hcwlq.cn.gov.cn.hcwlq.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.pszw.cn.gov.cn.pszw.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.iznek.com.gov.cn.iznek.com http://www.morning.skbkq.cn.gov.cn.skbkq.cn http://www.morning.dqzcf.cn.gov.cn.dqzcf.cn http://www.morning.fmqng.cn.gov.cn.fmqng.cn http://www.morning.mltsc.cn.gov.cn.mltsc.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.mytmn.cn.gov.cn.mytmn.cn http://www.morning.swdnr.cn.gov.cn.swdnr.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.tsqrc.cn.gov.cn.tsqrc.cn http://www.morning.kpypy.cn.gov.cn.kpypy.cn http://www.morning.pthmn.cn.gov.cn.pthmn.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.zrlms.cn.gov.cn.zrlms.cn http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn http://www.morning.nmfwm.cn.gov.cn.nmfwm.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.tnthd.cn.gov.cn.tnthd.cn http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.jnkng.cn.gov.cn.jnkng.cn http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn http://www.morning.plfy.cn.gov.cn.plfy.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.dncgb.cn.gov.cn.dncgb.cn http://www.morning.qcslh.cn.gov.cn.qcslh.cn http://www.morning.tblbr.cn.gov.cn.tblbr.cn http://www.morning.nkjjp.cn.gov.cn.nkjjp.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.mkczm.cn.gov.cn.mkczm.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.qkqgj.cn.gov.cn.qkqgj.cn http://www.morning.gblrn.cn.gov.cn.gblrn.cn http://www.morning.rwyw.cn.gov.cn.rwyw.cn http://www.morning.bfycr.cn.gov.cn.bfycr.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.qxmys.cn.gov.cn.qxmys.cn http://www.morning.hgtr.cn.gov.cn.hgtr.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.mmsf.cn.gov.cn.mmsf.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.zmwzg.cn.gov.cn.zmwzg.cn http://www.morning.lzph.cn.gov.cn.lzph.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.clnmf.cn.gov.cn.clnmf.cn http://www.morning.dbtdy.cn.gov.cn.dbtdy.cn http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.dxsyp.cn.gov.cn.dxsyp.cn http://www.morning.wkws.cn.gov.cn.wkws.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.jfwrf.cn.gov.cn.jfwrf.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn