当前位置:网站首页>剑指 Offer 34. 二叉树中和为某一值的路径-dfs法
剑指 Offer 34. 二叉树中和为某一值的路径-dfs法
2022-06-29 18:04:00 【Mr Gao】
剑指 Offer 34. 二叉树中和为某一值的路径
给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
输出:[[5,4,11,2],[5,8,4,5]]
示例 2:
输入:root = [1,2,3], targetSum = 5
输出:[]
示例 3:
输入:root = [1,2], targetSum = 0
输出:[]
这题其实比较常规的,做的时候,注意一下,一些细节,总体使用深度优先遍历去解决这题,可以遍历每条路径,并相对保留路径信息:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
/** * Return an array of arrays of size *returnSize. * The sizes of the arrays are returned as *returnColumnSizes array. * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). */
int path[1000];
int size;
void dfs(struct TreeNode* root,int target,int path_now_size,int sum,int **re,int** returnColumnSizes){
// printf("sum %d ",sum);
if(root){
// printf("val %d path_now_size %d",root->val);
path[path_now_size]=root->val;
sum=sum+root->val;
if(sum==target&&root->left==NULL&&root->right==NULL){
re[size]=(int *)malloc(sizeof(int)*(path_now_size+1));
(*returnColumnSizes)[size]=path_now_size+1;
int i;
for(i=0;i<path_now_size+1;i++){
// printf("%d ",path[i]);
re[size][i]=path[i];
}
size++;
}
dfs(root->left,target,path_now_size+1,sum,re,returnColumnSizes);
dfs(root->right,target,path_now_size+1,sum,re,returnColumnSizes);
}
}
#define maxsize 1000
int** pathSum(struct TreeNode* root, int target, int* returnSize, int** returnColumnSizes){
int**re=(int**)malloc(sizeof(int *)*maxsize);
*returnColumnSizes=(int*)malloc(sizeof(int )*maxsize);
size=0;
dfs(root,target,0,0,re,returnColumnSizes);
*returnSize=size;
printf("szie %d ",size);
return re;
}
边栏推荐
- [target tracking] |stark configuration win OTB
- 6.29模拟赛总结
- NVIDIA installs the latest graphics card driver
- 小白月赛51 补题 E G F
- Workflow module jar package startup error: liquibase – waiting for changelog lock
- Adobe Premiere foundation - sound adjustment (volume correction, noise reduction, telephone tone, pitch shifter, parameter equalizer) (XVIII)
- ABC253 D FizzBuzz Sum Hard(容斥定理)
- markdown常用字体
- 【TcaplusDB知识库】TcaplusDB运维单据介绍
- idea怎么使用?
猜你喜欢

Adobe Premiere foundation - material nesting (animation of Tiktok ending avatar) (IX)

mysql -connector/j驱动下载

Adobe Premiere Basics - common video effects (corner positioning, mosaic, blur, sharpen, handwriting tools, effect control hierarchy) (16)

How to use idea?

Premature end of script headers 或 End of script output before headers

Adobe Premiere基礎-聲音調整(音量矯正,降噪,電話音,音高換擋器,參數均衡器)(十八)

美法官裁定,被控掩盖黑客行为的Uber前安全主管必须面对欺诈指控

RocketMQ的tag过滤和sql过滤

Error building sqlsession problem

DevCloud加持下的青软,让教育“智”上云端
随机推荐
Adobe Premiere foundation - batch material import sequence - variable speed and rewind (recall) - continuous action shot switching - subtitle requirements (13)
Lodash deep copy usage
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
Shell基本语法--流程控制
Stepping on the pit: json Parse and json stringify
通过 hosts文件配置本地域名
Sister Juan takes you to learn database -- 5-day dash day4
You can do sideline work
MySql存储过程循环的使用分析详解
/usr/bin/ld: warning: **libmysqlclient. so. 20**, needed by //usr/
JWT login authentication
程序员资源推荐指南
[wangdingbei 2020 Qinglong formation]areuserialz
[target tracking] |stark configuration win OTB
【TcaplusDB知识库】TcaplusDB单据受理-建表审批介绍
工作流模块Jar包启动报错:liquibase – Waiting for changelog lock….
RocketMQ的tag过滤和sql过滤
Adobe Premiere基础-不透明度(混合模式)(十二)
Adobe Premiere Foundation - réglage du son (correction du volume, réduction du bruit, tonalité téléphonique, changement de hauteur, égaliseur de paramètres) (XVIII)
Maxcompute string replacement function -replace