513.找树左下角的值
题目链接
思路
要找最左下角的值,我们就需要得知二叉树的深度。自然而然想到层序遍历,遍历的第一个值就是最左下角的。
文章详解
cpp
/**
* 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) {
queue <TreeNode *> que;
//if(root == NULL) return 0;
que.push(root);
int leftval = 0;
while(!que.empty())
{
int size = que.size();
for(int i = 0; i < size; i++)
{
TreeNode * tmp = que.front();
que.pop();
if(i==0) leftval = tmp->val;
if(tmp->left) que.push(tmp->left);
if(tmp->right) que.push(tmp->right);
}
}
return leftval;
}
};
112 路径总和
题目链接
思路
结合之前做过的求所有路径的回溯方法,用递归法,不需要遍历所有节点所以,函数返回值为bool,入口参数为节点和剩余值。结束条件为,当前为叶子节点且剩余值为0,返回true;为叶子节点且值不为0,返回false. 单层循环逻辑:遍历左子树,回溯;遍历右子树,回溯。
文章详解
cpp
/**
* 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 traversal(TreeNode* node, int sum)
{
if(node->left == NULL && node->right == NULL && sum == 0) return true;
if(node->left == NULL && node->right == NULL) return false;
if(node->left)
{
if(traversal(node->left,sum - node->left->val)) return true;
}
if(node->right)
{
if(traversal(node->right,sum - node->right->val)) return true;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root == NULL) return false;
return traversal(root,targetSum - root->val);
}
};
112 路径总和
题目链接
思路
结合之前做过的求所有路径的回溯方法,用递归法,不需要遍历所有节点所以,函数返回值为bool,入口参数为节点和剩余值。结束条件为,当前为叶子节点且剩余值为0,返回true;为叶子节点且值不为0,返回false. 单层循环逻辑:遍历左子树,回溯;遍历右子树,回溯。
文章详解
c++
/**
* 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 traversal(TreeNode* node, int sum)
{
if(node->left == NULL && node->right == NULL && sum == 0) return true;
if(node->left == NULL && node->right == NULL) return false;
if(node->left)
{
if(traversal(node->left,sum - node->left->val)) return true;
}
if(node->right)
{
if(traversal(node->right,sum - node->right->val)) return true;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root == NULL) return false;
return traversal(root,targetSum - root->val);
}
};
113 路径总和ii
题目链接
思路
接近求所有路径之和的方法。
文章详解
cpp
/**
* 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 {
private:
vector<vector<int>> result;
vector<int> path;
// 递归函数不需要返回值,因为我们要遍历整个树
void traversal(TreeNode* cur, int count) {
if (!cur->left && !cur->right && count == 0) { // 遇到了叶子节点且找到了和为sum的路径
result.push_back(path);
return;
}
if (!cur->left && !cur->right) return ; // 遇到叶子节点而没有找到合适的边,直接返回
if (cur->left) { // 左 (空节点不遍历)
path.push_back(cur->left->val);
count -= cur->left->val;
traversal(cur->left, count); // 递归
count += cur->left->val; // 回溯
path.pop_back(); // 回溯
}
if (cur->right) { // 右 (空节点不遍历)
path.push_back(cur->right->val);
count -= cur->right->val;
traversal(cur->right, count); // 递归
count += cur->right->val; // 回溯
path.pop_back(); // 回溯
}
return ;
}
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
result.clear();
path.clear();
if (root == NULL) return result;
path.push_back(root->val); // 把根节点放进路径
traversal(root, sum - root->val);
return result;
}
};
106 二叉树:从中序与后序遍历序列构造二叉树
题目链接
106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)
思路
基本思路是从中序和后序拆分序列,找到根、左子树、右子树。后序序列的最后一个为根,找到中序中该节点对应位置,前面的是左子树,后面的是右子树。依次类推,逐层拆解。
文章详解
cpp
todo