目标
过程
- 513.找树左下角的值
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode* >que;
TreeNode* left_node=root;
que.push(root);
while(!que.empty()){
TreeNode* temp_node=que.front();
left_node=temp_node;
que.pop();
if(temp_node->right){
que.push(temp_node->right);
}
if(temp_node->left){
que.push(temp_node->left);
}
}
return left_node->val;
}
};
class Solution {
public:
bool hasPathSum_recursion(TreeNode* root, int targetSum,int cur_sum){
cur_sum+=root->val;
// if(cur_sum>targetSum){
// return false;
// }
if((root->left==NULL) &&(root->right==NULL)){
if(cur_sum==targetSum){
return true;
}
else{
return false;
}
}
bool temp_bool=false;
if(root->left){
temp_bool=hasPathSum_recursion(root->left,targetSum,cur_sum);
}
if(root->right){
temp_bool|=hasPathSum_recursion(root->right,targetSum,cur_sum);
}
return temp_bool;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root==NULL){
return false;
}
return hasPathSum_recursion(root,targetSum,0);
}
};
112. 路径总和
class Solution {
public:
void pathSum_recursion(TreeNode* root, int cur_sum,int targetSum,vector<int> path_vector){
cur_sum+=root->val;
path_vector.push_back(root->val);
if((root->left==NULL)&&(root->right==NULL)){
//表明这个是叶节点
if(cur_sum==targetSum){
target_vector.push_back(path_vector);
}
return ;
}
if(root->left!=NULL){
pathSum_recursion(root->left,cur_sum,targetSum,path_vector);
}
if(root->right!=NULL){
pathSum_recursion(root->right,cur_sum,targetSum,path_vector);
}
}
vector<vector<int>> target_vector;
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
int cur_sum=0;
vector<int> path_vector;
if(root){
pathSum_recursion(root,cur_sum,targetSum,path_vector);
}
return target_vector;
}
};
106.从中序与后序遍历序列构造二叉树
class Solution {
public:
TreeNode* traversal (vector<int>& inorder, vector<int>& postorder) {
if (postorder.size() == 0) return NULL;
// 后序遍历数组最后一个元素,就是当前的中间节点
int rootValue = postorder[postorder.size() - 1];
TreeNode* root = new TreeNode(rootValue);
// 叶子节点
if (postorder.size() == 1) return root;
// 找到中序遍历的切割点
int delimiterIndex;
for (delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++) {
if (inorder[delimiterIndex] == rootValue) break;
}
// 切割中序数组
// 左闭右开区间:[0, delimiterIndex)
vector<int> leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);
// [delimiterIndex + 1, end)
vector<int> rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end() );
// postorder 舍弃末尾元素
postorder.resize(postorder.size() - 1);
// 切割后序数组
// 依然左闭右开,注意这里使用了左中序数组大小作为切割点
// [0, leftInorder.size)
vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
// [leftInorder.size(), end)
vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());
root->left = traversal(leftInorder, leftPostorder);
root->right = traversal(rightInorder, rightPostorder);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
//中序和后序遍历二叉树
// 第一步
if (inorder.size() == 0 || postorder.size() == 0) return NULL;
return traversal(inorder, postorder);
}
};
105.从前序与中序遍历序列构造二叉树 一起做,思路一样的
class Solution {
public:
TreeNode* traversal ( vector<int>& preorder, int pre_start,int pre_end,vector<int>& inorder,int in_start,int in_end) {
if(pre_end<=pre_start){
return NULL;
}
// 获取前序的第一个元素
int rootValue = preorder[pre_start];
TreeNode* root = new TreeNode(rootValue);
// 叶子节点 表明是叶子节点
if(pre_end-pre_start<=1)
return root;
// 找到中序遍历的切割点
int delimiterIndex;
for (delimiterIndex = in_start; delimiterIndex < in_end; delimiterIndex++) {
if (inorder[delimiterIndex] == rootValue) break;
}
// 切割中序数组
// 左闭右开区间:[0, delimiterIndex)
int left_size=delimiterIndex-in_start;
int right_size=in_end-delimiterIndex;
root->left = traversal(preorder,pre_start+1,pre_start+1+left_size, inorder,in_start,in_start+left_size);
root->right = traversal(preorder,pre_start+1+left_size,pre_end, inorder,in_start+left_size+1,in_end);
return root;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(preorder.size()==0 && inorder.size()==0 )
return NULL;
return traversal(preorder,0,preorder.size(),inorder,0,inorder.size());
}
};
结果
花了好长时间才做完。