代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。