513.找树左下角的值
层次遍历:
function findBottomLeftValue(root: TreeNode | null): number {
let helperQueue = [root];
let result: number;
while(helperQueue.length > 0){
let len = helperQueue.length;
for(let i = 0; i < len; i++){
let node = helperQueue.shift();
if(i === 0){
result = node.val;
}
node.left && helperQueue.push(node.left);
node.right && helperQueue.push(node.right);
}
}
return result;
};
官方题解:递归法
function findBottomLeftValue(root: TreeNode | null): number {
function recur(root: TreeNode, depth: number): void {
if (root.left === null && root.right === null) {
if (depth > maxDepth) {
maxDepth = depth;
resVal = root.val;
}
return;
}
if (root.left !== null) recur(root.left, depth + 1);
if (root.right !== null) recur(root.right, depth + 1);
}
let maxDepth: number = 0;
let resVal: number = 0;
if (root === null) return resVal;
recur(root, 1);
return resVal;
};
112. 路径总和
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
if (!root) return false;
let sum = 0;
function traverse(root: TreeNode | null) {
sum += root.val;
if (!root.left && !root.right) {
if (sum === targetSum) return true;
else {
sum -= root.val;
return false;
}
}
let isHasTargetSum = false;
if (root.left) isHasTargetSum = traverse(root.left);
if (isHasTargetSum) return true;
if (root.right) isHasTargetSum = traverse(root.right);
if (isHasTargetSum) return true;
sum -= root.val;
return false;
}
return traverse(root);
};
官方题解:
递归(精简版)
function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
if (root === null) return false;
targetSum -= root.val;
if (
root.left === null &&
root.right === null &&
targetSum === 0
) return true;
return hasPathSum(root.left, targetSum) ||
hasPathSum(root.right, targetSum);
};
113. 路径总和 ||
function pathSum(root: TreeNode | null, targetSum: number): number[][] {
if(!root) return [];
let results = [];
let path = [];
function traverse(root: TreeNode | null, targetSum: number){
path.push(root.val);
if(!root.left && !root.right && (targetSum - root.val === 0)){
results.push([...path]);
path.pop();
return;
}
if(!root.left && !root.right){
path.pop();
return;
}
root.left && traverse(root.left, targetSum - root.val);
root.right && traverse(root.right, targetSum - root.val);
path.pop();
return;
}
traverse(root, targetSum);
return results;
};
这里没有必要要了:
if(!root.left && !root.right){
path.pop();
return;
}
function pathSum(root: TreeNode | null, targetSum: number): number[][] {
if(!root) return [];
let results = [];
let path = [];
function traverse(root: TreeNode | null, targetSum: number){
path.push(root.val);
if(!root.left && !root.right && (targetSum - root.val === 0)){
results.push([...path]);
path.pop();
return;
}
root.left && traverse(root.left, targetSum - root.val);
root.right && traverse(root.right, targetSum - root.val);
path.pop();
return;
}
traverse(root, targetSum);
return results;
};
106.从中序与后序遍历序列构造二叉树
function buildTree(inorder: number[], postorder: number[]): TreeNode | null {
if(inorder.length === 0) return null;
let inorderLeftArr = [];
let inorderRightArr = [];
let postorderLeftArr = [];
let postorderRightArr = [];
let isLeft = true;
let curNode = new TreeNode(postorder.pop());
for (let num of inorder) {
if (num === curNode.val) {
isLeft = false;
continue;
}
if (isLeft) inorderLeftArr.push(num);
else inorderRightArr.push(num);
}
postorderLeftArr = postorder.slice(0, inorderLeftArr.length);
postorderRightArr = postorder.slice(inorderLeftArr.length, postorder.length);
curNode.left = buildTree(inorderLeftArr, postorderLeftArr);
curNode.right = buildTree(inorderRightArr, postorderRightArr);
return curNode;
};
官方题解:
function buildTree(inorder: number[], postorder: number[]): TreeNode | null {
if (postorder.length === 0) return null;
const rootVal: number = postorder.pop()!;
const rootValIndex: number = inorder.indexOf(rootVal);
const rootNode: TreeNode = new TreeNode(rootVal);
rootNode.left = buildTree(inorder.slice(0, rootValIndex), postorder.slice(0, rootValIndex));
rootNode.right = buildTree(inorder.slice(rootValIndex + 1), postorder.slice(rootValIndex));
return rootNode;
};
105.从前序与中序遍历序列构造二叉树
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
if(preorder.length === 0) return null;
const node = new TreeNode(preorder.shift());
let nodeIndex = inorder.indexOf(node.val);
node.left = buildTree(preorder.slice(0, nodeIndex), inorder.slice(0, nodeIndex));
node.right = buildTree(preorder.slice(nodeIndex), inorder.slice(nodeIndex + 1));
return node;
};