@ 代码随想录算法训练营第三周(C语言)|Day18(二叉树)
Day18、二叉树(513.找树左下角的值 112. 路径总和 113.路径总和ii 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树 )
513.找树左下角的值
题目描述
给定一个二叉树,在树的最后一行找到最左边的值。
题目解答
int findBottomLeftValue(struct TreeNode* root) {
int res;
struct TreeNode* queue[10000];
int front=0;
int rear=1;
queue[0]=root;
while(front<rear){
int len=rear-front;
for(int i=0;i<len;i++){
struct TreeNode*tmp=queue[front++];
if(i==0){
res=tmp->val;
}
if(tmp->left){
queue[rear++]=tmp->left;
}
if(tmp->right){
queue[rear++]=tmp->right;
}
}
}
return res;
}
//digui
void huisu(struct TreeNode*root,int depth,int *maxdepth,int *res){
if(root->left==NULL&&root->right==NULL){
if(*maxdepth<depth){
*maxdepth=depth;
*res=root->val;
}
}
if(root->left){
depth++;
huisu(root->left,depth,maxdepth,res);
depth--;
}
if(root->right){
depth++;
huisu(root->right,depth,maxdepth,res);
depth--;
}
}
int findBottomLeftValue(struct TreeNode* root) {
if(root==NULL){
return 0;
}
int depth=0;
int maxdepth=-1;
int res=0;
huisu(root,depth,&maxdepth,&res);
return res;
}
题目总结
huisu(root,depth,&maxdepth,&res);加入深度参数。
112. 路径总和 113.路径总和ii
题目描述
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
题目解答
//112
bool ishaspathsum(struct TreeNode*root,int targetSum){
if (root == NULL) {
return false;
}
if(root->left==NULL&&root->right==NULL&&targetSum==0){
return true;
}
if(root->left==NULL&&root->right==NULL){
return false;
}
if(root->left){
targetSum-=root->left->val;
if(ishaspathsum(root->left,targetSum)){
return true;
}
targetSum+=root->left->val;
}
if(root&&root->right){
targetSum-=root->right-> val;
if(ishaspathsum(root->right,targetSum)){
return true;
}
targetSum+=root->right-> val;
}
return false;
}
bool hasPathSum(struct TreeNode* root, int targetSum) {
if (root == NULL) {
return false;
}
if(ishaspathsum(root,targetSum-root->val)){
return true;
}else{
return false;
}
}
//113
int **res;
int ressize;
int *rescolsize;
int *path;
int pathsize;
void dfs(struct TreeNode*root,int targetSum){
if(root==NULL){
return;
}
path[pathsize++]=root->val;
targetSum-=root->val;
if(root->left==NULL&&root->right==NULL&&targetSum==0){
int*tmp=malloc(sizeof(int)*pathsize);
memcpy(tmp,path,sizeof(int)*pathsize);
res[ressize]=tmp;
rescolsize[ressize++]=pathsize;
}
dfs(root->left,targetSum);
dfs(root->right,targetSum);
pathsize--;
}
int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** returnColumnSizes) {
res=malloc(sizeof(int*)*2001);
rescolsize=malloc(sizeof(int)*2001);
path=malloc(sizeof(int)*2001);
ressize=pathsize=0;
dfs(root,targetSum);
*returnColumnSizes=rescolsize;
*returnSize=ressize;
return res;
}
题目总结
注意全局变量的作用。
105.从前序与中序遍历序列构造二叉树
题目描述
根据一棵树的中序遍历与后序遍历构造二叉树。
题目解答
//105
struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize) {
struct TreeNode* root=(struct TreeNode*)malloc(sizeof(struct TreeNode));
root->left=NULL;
root->right=NULL;//root->left和root->right指针赋初值也没有置为NULL。
if(postorderSize==0){
return NULL;
}
root->val=postorder[postorderSize-1];
if(postorderSize==1){
return root;
}
int index=0;
for(;index<inorderSize;index++){
if(inorder[index]==root->val){
break;
}
}
// int inorderleft[index];
// int inorderright[inorderSize-index-1];
// for(int i=0;i<index;i++){
// inorderleft[i]=inorder[i];
// }
// for(int i=0;i<inorderSize-index-1;i++){
// inorderright[i]=inorder[index+1+i];
// }
// int postorderleft[index];
// int postorderright[inorderSize-index-1];
// for(int i=0;i<index;i++){
// postorderleft[i]=postorder[i];
// }
// for(int i=0;i<inorderSize-index-1;i++){
// postorderright[i]=postorder[index+i];
// }
root->left=buildTree(inorder,index,postorder,index);
root->right=buildTree(inorder+index+1,inorderSize-index-1,postorder+index,inorderSize-index-1);
return root;
}
题目总结
利用地址分割数组。