当前位置:网站首页>力扣练习——44 路径总和 III
力扣练习——44 路径总和 III
2022-08-02 04:18:00 【qq_43403657】
44 路径总和 III
1.问题描述
给定一个二叉树,它的每个结点都存放着一个整数值。
找出路径和等于给定数值的路径总数。
路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。
二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数。
示例:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1
返回 3。和等于 8 的路径有:
5 -> 3
5 -> 2 -> 1
-3 -> 11
可使用以下main函数:
#include
#include
#include
#include
#include
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(NULL), right(NULL) {}
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
TreeNode* inputTree()
{
int n,count=0;
char item[100];
cin>>n;
if (n==0)
return NULL;
cin>>item;
TreeNode* root = new TreeNode(atoi(item));
count++;
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (count<n)
{
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
cin>>item;
count++;
if (strcmp(item,"null")!=0)
{
int leftNumber = atoi(item);
node->left = new TreeNode(leftNumber);
nodeQueue.push(node->left);
}
if (count==n)
break;
cin>>item;
count++;
if (strcmp(item,"null")!=0)
{
int rightNumber = atoi(item);
node->right = new TreeNode(rightNumber);
nodeQueue.push(node->right);
}
}
return root;
}
int main()
{
TreeNode* root;
root=inputTree();
int sum;
cin>>sum;
int res=Solution().pathSum(root,sum);
cout<<res<<endl;
}
2.输入说明
首先输入结点的数目n(注意,这里的结点包括题中的null空结点)
然后输入n个结点的数据,需要填充为空的结点,输入null。
最后输入一个整数sum。
3.输出说明
输出一个整数,表示结果。
4.范例
输入
11
10 5 -3 3 2 null 11 3 -2 null 1
8
输出
3
5.代码
#include <iostream>
#include <queue>
#include <cstdlib>
#include <cstring>
#include<map>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(NULL), right(NULL) {
}
TreeNode(int x) : val(x), left(NULL), right(NULL) {
}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {
}
};
TreeNode* inputTree()
{
int n, count = 0;
char item[100];
cin >> n;
if (n == 0)
return NULL;
cin >> item;
TreeNode* root = new TreeNode(atoi(item));
count++;
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (count < n)
{
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
cin >> item;
count++;
if (strcmp(item, "null") != 0)
{
int leftNumber = atoi(item);
node->left = new TreeNode(leftNumber);
nodeQueue.push(node->left);
}
if (count == n)
break;
cin >> item;
count++;
if (strcmp(item, "null") != 0)
{
int rightNumber = atoi(item);
node->right = new TreeNode(rightNumber);
nodeQueue.push(node->right);
}
}
return root;
}
int rootSum(TreeNode *root, int sum)
{
if (!root)//空树
return 0;
int res = 0;
if (root->val == sum)//根节点正好等于sum
res++;
res += rootSum(root->left, sum - root->val);//继续遍历左子树
res += rootSum(root->right, sum - root->val);//继续遍历右子树
return res;
}
int pathSum(TreeNode *root, int sum)
{
if (!root)
return 0;
int res = rootSum(root, sum);//以该根节点开始遍历
res += pathSum(root->left, sum);//以左子树为根节点开始遍历
res += pathSum(root->right, sum);//以右子树为根节点开始遍历
return res;
}
int main()
{
TreeNode* root;
root = inputTree();
int sum;
cin >> sum;
int res = pathSum(root, sum);
cout << res << endl;
}
边栏推荐
猜你喜欢
internship:数据库表和建立的实体类及对应的枚举类之间的联系示例
Line generation 005
论文速读:Homography Loss for Monocular 3D Object Detection
Arduino框架下STM32F1/F4系列HID模式程序烧录教程
Platts Analysis-MATLAB Toolbox Function
复制延迟案例(2)-读己之写
Live | 7.30 ApacheCon Asia 2022 IOT/IIOT topic, IoTDB PMC Qiao Jialin as the producer
直播 | 7.30 ApacheCon Asia 2022 IOT/IIOT专题,IoTDB PMC 乔嘉林担任出品人
Visual SLAM Lecture Fourteen - Lecture 13 Practice: Designing a SLAM system (the most detailed code debugging and running steps)
多主复制下处理写冲突(4)-多主复制拓扑
随机推荐
张成分析(spanning test):portfolio_analysis.Spanning_test
跑通CogView教程
Scala basics [common method supplement, pattern matching]
【Interview】Recruitment requirements
ScholarOne Manuscripts submits journal LaTeX file and cannot convert PDF successfully!
How to decrypt worksheet protection in Excel
压缩包密码如何快速删除?
应用pca和K-means实现用户对物品类别的喜好细分划分
Visual SLAM Lecture Fourteen - Lecture 13 Practice: Designing a SLAM system (the most detailed code debugging and running steps)
力扣 剑指 Offer 56 - I. 数组中数字出现的次数
falco 【1】入门
C程序调试过程常见的错误
Deep Blue Academy - Handwritten VIO Homework - Chapter 2
AFMG SysTune1.3.7使用图解
Qt常见问题
七月阅读:《刘慈欣科幻短篇小说集Ⅰ》笔记
力扣练习——38 分割回文串
今天突然下雨
“数字化重构系统,搞定 CEO 是第一步”
自定义一个下划线分词器