十一、左叶子之和
题目
给定二叉树的根节点 root
,返回所有左叶子之和。
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
// 递归停止条件
if(root == nullptr) return 0;
if(root->left == nullptr && root->right == nullptr) return 0;
// 单层迭代条件,当遇到符合条件则赋值 左
int left = sumOfLeftLeaves(root->left);
if(root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr){
left = root->left->val;
}
// 右
int right = sumOfLeftLeaves(root->right);
// 中
return right + left;
}
};
十二、找树左下角的值
题目
给定一个二叉树的 根节点 root
,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
题解 – 层次遍历
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> q;
int result;
if(root == nullptr) return result;
q.push(root);
while(!q.empty()){
int size = q.size();
vector<int> vec;
while(size--){
TreeNode* tmp = q.front();
q.pop();
vec.push_back(tmp->val);
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
result = vec[0];
}
return result;
}
};
题解 – 递归
class Solution {
public:
int maxDepth = INT_MIN;
int result;
void traverse(TreeNode* root, int depth){
if(root->left == nullptr && root->right == nullptr){
if(depth > maxDepth){
maxDepth = depth;
result = root->val;
}
return;
}
if(root->left){
depth++;
traverse(root->left, depth);
depth--;
}
if(root->right){
depth++;
traverse(root->right, depth);
depth--;
}
}
int findBottomLeftValue(TreeNode* root) {
traverse(root, 0);
return result;
}
};
十三、路径总和
题目
给你二叉树的根节点 root
和一个表示目标和的整数 targetSum
。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum
。如果存在,返回 true
;否则,返回 false
。
叶子节点 是指没有子节点的节点
题解
class Solution {
public:
bool traverse(TreeNode* cur, int count){
if(!cur->left && !cur->right && count == 0) return true;
// 没有找到合适的边
if(!cur->left && !cur->right) return false;
if(cur->left){
count -= cur->left->val;
if(traverse(cur->left, count)) return true;
count += cur->left->val;
}
if(cur->right){
count -= cur->right->val;
if(traverse(cur->right, count)) return true;;
count += cur->right->val;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root == nullptr) return false;
return traverse(root, targetSum - root->val);
}
};
十四、从中序与后序遍历序列构造二叉树
一定要主要区间统一性~
- 后序数组最后一个元素为中
- 切割中序数组
- 分成左右中序数组
- 后序数组resize
- 分成左右后序数组
- traverse
题目
给定两个整数数组 inorder
和 postorder
,其中 inorder
是二叉树的中序遍历, postorder
是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)
class Solution {
public:
TreeNode* traverse(vector<int>& inorder, vector<int>& postorder){
if(postorder.size() == 0) return nullptr;
// 拿到后序遍历的最后一个元素
TreeNode* root = new TreeNode(postorder[postorder.size() - 1]);
if(postorder.size() == 1) return root;
// 找到中序遍历的切割点
int divideIndex = 0;
for(; divideIndex < inorder.size(); divideIndex++){
if(inorder[divideIndex] == postorder[postorder.size() - 1]) break;
}
// 左闭右开
vector<int> leftInorder(inorder.begin(), inorder.begin() + divideIndex);
vector<int> rightInorder(inorder.begin() + divideIndex + 1, inorder.end());
// 后序
postorder.resize(postorder.size() - 1);
vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());
root->left = traverse(leftInorder, leftPostorder);
root->right = traverse(rightInorder, rightPostorder);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size() == 0 || postorder.size() == 0) return nullptr;
return traverse(inorder, postorder);
}
};