题目:
给定一个二叉树的 根节点 root
,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点
/**
* 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:
//bfs
// int findBottomLeftValue(TreeNode* root) {
// queue<TreeNode*> que;
// if(root) que.push(root);
// int res=0;
// while(!que.empty()){
// int size, layerSize ;
// size = layerSize = que.size();
// while(size--){
// TreeNode* ptr = que.front();
// if(size==layerSize-1) res=ptr->val;
// que.pop();
// if(ptr->left) que.push(ptr->left);
// if(ptr->right) que.push(ptr->right);
// }
// }
// return res;
// }
//dfs
public:
int res=0;
int maxDepth = INT_MIN;
//1.明确语义:处理以node为根节点的子树-》记录当前节点深度、并且维护最深、最左叶子节点的值
void tranverse(TreeNode*node, int depth){
//2. 确定边界:当遇到叶子节点后return 并且根据记录的深度值,存储最深处的叶子节点值
if(node->left==nullptr&&node->right==nullptr){
if(depth>maxDepth){
maxDepth = depth;
res = node->val;
}
return;
}
//3. 确定单层递归的逻辑:
// 3.1先遍历左子树,再遍历右子树,这样使得
// 同样的深度,只会存储最左侧的叶子节点值;(如果求最底层、最右边的叶子节点值,则先右后左子树遍历)
// 3.2 因为每进入一层递归,相当于进入更深的子树进行遍历,所以传入的depth++;
// 而递归返回到当前层后,需要恢复到原来的深度值(depth--),这是一个回溯行为;
// 当单层逻辑走完,return回上一层
if(node->left){
depth++;
tranverse(node->left,depth);
depth--;
}
if(node->right){
depth++;
tranverse(node->right,depth);
depth--;
}
return;
}
int findBottomLeftValue(TreeNode* root) {
if(root){
tranverse(root, 1);
return res;
}
else{
return 0;
}
}
};
题目:
给你二叉树的根节点 root
和一个表示目标和的整数 targetSum
。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum
。如果存在,返回 true
;否则,返回 false
。
叶子节点 是指没有子节点的节点。
/**
* 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 isPathSum(TreeNode* root, int count){
if(root->left==nullptr&&root->right==nullptr&&count==0) return true;
if(root->left==nullptr&&root->right==nullptr&&count!=0) return false;
if(root->left) {
count-=root->left->val;
if(isPathSum(root->left,count)) return true;
count+=root->left->val;;
}
if(root->right){
count-=root->right->val;
if(isPathSum(root->right,count)) return true;
count+=root->right->val;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root) return isPathSum(root,targetSum-root->val);
else return false;
}
};
题目:
给你二叉树的根节点 root
和一个整数目标和 targetSum
,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
/**
* 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:
void getPathSum(TreeNode* root, int count, vector<vector<int>>& res, vector<int> path){
if(root->left==nullptr&&root->right==nullptr&&count==0) {
res.push_back(path);
return;
}
if(root->left==nullptr&&root->right==nullptr&&count!=0) return;
if(root->left) {
count-=root->left->val;
path.push_back(root->left->val);
getPathSum(root->left,count,res,path);
path.pop_back();
count+=root->left->val;;
}
if(root->right){
count-=root->right->val;
path.push_back(root->right->val);
getPathSum(root->right,count,res,path);
path.pop_back();
count+=root->right->val;
}
return;
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> res;
vector<int> path;
if(root) {
path.push_back(root->val);
getPathSum(root,targetSum-root->val,res,path);
}
return res;
}
};
106暂时周末补上