当前位置:网站首页>剑指 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;
}
边栏推荐
- EasyCVR部署服务器集群时,出现一台在线一台不在线是什么原因?
- Jar package background startup and log output
- Adobe Premiere基础-批量素材导入序列-变速和倒放(回忆)-连续动作镜头切换-字幕要求(十三)
- Image migration and data migration synchronization of old and new servers with different Alibaba cloud accounts
- 字典树(随学)
- Adobe Premiere foundation - opacity (matte) (11)
- Goldfish rhca memoirs: do447 build advanced job workflow -- create job template survey to set work variables
- 等保测评结论为差,是不是表示等保工作白做了?
- Sd6.24 summary of intensive training
- 给定一个数在序列中求最大异或值(01字典)
猜你喜欢

Precondition end of script headers or end of script output before headers
![[target tracking] |stark configuration win OTB](/img/29/a6b3b99b7d2349499aede9e76ab29a.png)
[target tracking] |stark configuration win OTB

踩坑记:JSON.parse和JSON.stringify

Travel card "star picking" hot search first! Stimulate the search volume of tourism products to rise

Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)

Adobe Premiere基础-常用的视频特效(边角定位,马赛克,模糊,锐化,手写工具,效果控件层级顺序)(十六)

Let Google search your blog

Abc253 D fizzbuzz sum hard (tolerance exclusion theorem)

【网络是怎么连接的】第三章 探索集线器,交换机和路由器

jdbc_相关代码
随机推荐
Request header field XXXX is not allowed by access control allow headers in preflight response
Proxmox VE Install 7.2
3H proficient in opencv (VIII) - shape detection
小白月赛51 补题 E G F
Error building sqlsession problem
Xiaobai yuesai 51 supplement e g f
Adobe Premiere基础-批量素材导入序列-变速和倒放(回忆)-连续动作镜头切换-字幕要求(十三)
Wechat applet development reserve knowledge
[target tracking] |stark configuration win OTB
【TcaplusDB知识库】TcaplusDB单据受理-创建游戏区介绍
保持jupyter notebook在终端关闭时的连接方法
Abc253 D fizzbuzz sum hard (tolerance exclusion theorem)
Jar package background startup and log output
codeforces每日5题(均1700)-第二天
lodash深拷贝使用
You can do sideline work
源码安装MAVROS
My first experience of remote office | community essay solicitation
DevCloud加持下的青软,让教育“智”上云端
Longest XOR path (dfs+01trie)