一、路径总和
题目一:112. 路径总和
给你二叉树的根节点 root
和一个表示目标和的整数 targetSum
。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum
。如果存在,返回 true
;否则,返回 false
。
叶子节点 是指没有子节点的节点。
函数首先检查根节点是否为空。如果为空,返回
false
。然后,检查当前节点是否是叶子节点(即没有子节点)。
如果是叶子节点,并且其值等于剩余的目标和(即
targetSum - root->val
),则表示找到了一条符合条件的路径,返回true
。如果当前节点不是叶子节点,函数递归地在左子树和右子树中寻找符合条件的路径。
/*
* @lc app=leetcode.cn id=112 lang=cpp
*
* [112] 路径总和
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if (root == nullptr) {
return false;
}
if (root->left == nullptr && root->right == nullptr) {
return root->val == targetSum;
}
return hasPathSum(root->left, targetSum - root->val)
|| hasPathSum(root->right, targetSum - root->val);
}
};
// @lc code=end
二、找树左下角的值
题目一:513. 找树左下角的值
给定一个二叉树的 根节点 root
,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
使用一个队列来进行层序遍历。每次处理一层节点时,取出队列中的所有节点,并将它们的子节点加入队列。
同时,记录下每一层的第一个节点的值。当所有层都被遍历完成后,最后记录的值即为最底层最左边节点的值。
/*
* @lc app=leetcode.cn id=513 lang=cpp
*
* [513] 找树左下角的值
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
if (!root) return -1;
queue<TreeNode*> q;
q.push(root);
int result = 0;
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; ++i) {
TreeNode* node = q.front();
q.pop();
if (i == 0) result = node->val;
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return result;
}
};
// @lc code=end
三、从中序与后序遍历序列构造二叉树
题目一:106. 从中序与后序遍历序列构造二叉树
给定两个整数数组 inorder
和 postorder
,其中 inorder
是二叉树的中序遍历, postorder
是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
在这个实现中,首先创建一个哈希表来存储中序遍历中每个值对应的索引,这样可以在构建树时快速定位根节点在中序遍历中的位置。
然后,使用递归方法构建树。对于后序遍历中的最后一个元素,它是当前子树的根节点。
找到这个根节点在中序遍历中的位置,这样就可以确定左右子树的范围,并递归地对左右子树进行同样的操作。
/*
* @lc app=leetcode.cn id=106 lang=cpp
*
* [106] 从中序与后序遍历序列构造二叉树
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
unordered_map<int, int> inorderMap;
for (int i = 0; i < inorder.size(); ++i) {
inorderMap[inorder[i]] = i;
}
return buildTree(postorder, 0, postorder.size() - 1, inorderMap, 0, inorder.size() - 1);
}
private:
TreeNode* buildTree(vector<int>& postorder, int postStart, int postEnd,
unordered_map<int, int>& inorderMap, int inStart, int inEnd) {
if (postStart > postEnd || inStart > inEnd) return nullptr;
TreeNode* root = new TreeNode(postorder[postEnd]);
int inRoot = inorderMap[root->val];
int numsLeft = inRoot - inStart;
root->left = buildTree(postorder, postStart, postStart + numsLeft - 1, inorderMap, inStart, inRoot - 1);
root->right = buildTree(postorder, postStart + numsLeft, postEnd - 1, inorderMap, inRoot + 1, inEnd);
return root;
}
};
// @lc code=end