二叉树 part05
找树左下角的值
给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
输入: root = [2,1,3]
输出: 1
层序遍历,返回最下层第一个元素;
/**
* 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 {number}
*/
var findBottomLeftValue = function(root) {
if(root == null){
return
}
let queue = [];
let result = [];
queue.push(root);
while(queue.length > 0){
let arr = [];
let size = queue.length;
while(size--){
let node = queue.shift();
arr.push(node.val);
if(node.left != null){
queue.push(node.left);
}
if(node.right != null){
queue.push(node.right);
}
}
result.push(arr);
}
return result.pop().shift();
};
递归法:
var findBottomLeftValue = function(root) {
if(root == null){
return
}
let maxDepth = Number.MIN_VALUE;
let result;
let depth = 1;
var LeftValue = function(root,depth){
if(root.left == null && root.right == null){
if(depth > maxDepth){
maxDepth = depth;
result = root.val;
}
return;
}
if(root.left != null){
depth += 1;
LeftValue(root.left,depth);
depth -= 1;
}
if(root.right != null){
depth += 1;
LeftValue(root.right,depth);
depth -= 1;
}
// return result;
}
LeftValue(root,depth,depth);
return result;
};
let testTree = {
val:0,
right:{val:-1},
}
// left:{val:2,left:{val:3}},
findBottomLeftValue(testTree);
路径总和
给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。
叶子节点 是指没有子节点的节点。
输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
输出:true
解释:等于目标和的根节点到叶节点路径如上图所示。
遍历的路线,并不要遍历整棵树,所以递归函数需要返回值,可以用bool类型表示。
/**
* 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
* @param {number} targetSum
* @return {boolean}
*/
var hasPathSum = function(root, targetSum) {
if(root == null){
return false;
}
return isHasPathSum(root,targetSum-root.val);
};
var isHasPathSum = function(root,count){
if(root.left == null && root.right == null && count == 0){
//遇到叶子节点,并且计数为0返回true
return true;
}
if(root.left == null && root.right == null && count != 0){
//遇到叶子节点,计数不为0返回false
return false;
}
if(root.left != null){
//左递归
count = count - root.left.val;
if(isHasPathSum(root.left,count)){
return true;
}
count = count + root.left.val; //回溯,撤销处理结果
}
if(root.right != null){
//右递归
count = count - root.right.val;
if(isHasPathSum(root.right,count)){
return true;
}
count = count + root.right.val;//回溯,撤销处理结果
}
return false;
}
let testTree = {
val:1,
left:{val:2},
right:{val:4},
}
hasPathSum(testTree,3);
从中序与后序遍历序列构造二叉树
给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出:[3,9,20,null,null,15,7]
1.如果数组为空,返回空节点;
2.如果不为空,取后序数组最后一个元素作为根节点;
3.找到后序数组最后一个元素在中序数组中的位置,作为切割点;
4.切割中序数组,分为中序左数组,中序右数组;
5.中序左右数组切割完后,可确定左右数组长度,切割后序左右数组,
6.递归处理左区间和右区间;
var buildTree = function(inorder, postorder) {
if(postorder.length == 0){
return null;
}
let rootvalue = postorder[postorder.length - 1];
let root = new TreeNode(rootvalue);
// root.val = rootvalue;
if(postorder.length == 1){
return root;
}
let index = 0;
for(index= 0;index < inorder.length;index++){
if(inorder[index] == rootvalue){
break;
}
}
let inorderArr1 = inorder.slice(0,index);
let inorderArr2 = inorder.slice(index + 1,inorder.length);
let postorderArr1 = postorder.slice(0,inorderArr1.length);
let postorderArr2 = postorder.slice(inorderArr1.length,postorder.length-1);
root.left = buildTree(inorderArr1,postorderArr1);
root.right = buildTree(inorderArr2,postorderArr2);
return root;
};
let inorder = [9,3,15,20,7];
let postorder = [9,15,7,20,3];
buildTree(inorder,postorder);