1 Star 0 Fork 0

duanfaxin/origin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
_2022_07_10.cpp 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
duanfaxin 提交于 2022-07-10 16:34 . 20220710
#include<iostream>
#include<vector>
using namespace std;
//分治的思想
class Solution {
public:
bool Postorder(vector<int>& postorder,int start,int end)
{
if(start>end) return true;
int index = start;
while(postorder[index]<postorder[end])index++;
int mid = index;
while(postorder[index]>postorder[end])index++;
bool left = Postorder(postorder,start,mid-1);
bool right = Postorder(postorder,mid,end-1);
return index==end&&left&&right;
}
bool verifyPostorder(vector<int>& postorder) {
return Postorder(postorder,0,postorder.size()-1);
}
};
///dfs
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
vector<vector<int>> ans;
vector<int> temp;
bool f = true;
void dfs(TreeNode* root,int target,int count)
{
if(!root) return ;
temp.push_back(root->val);
count+=root->val;
if(!root->left&&!root->right&&count == target)
ans.push_back(temp);
dfs(root->left,target,count);
dfs(root->right,target,count);
temp.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int target) {
if(!root)return ans;
dfs(root,target,0);
return ans;
}
};
int main()
{
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/duanfaxin/origin.git
[email protected]:duanfaxin/origin.git
duanfaxin
origin
origin
master

搜索帮助