-
思路 以及算法过程:
- code:
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<unordered_map>
#include<queue>
#include<stack>
#include<string>
#include <set>
#include <unordered_set>
#include <map>
//* Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
//Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}
//my
class Solution {
public:
vector<TreeNode *> stk;
TreeNode* maxTree(vector<int> A) {
vector<TreeNode*> tempM;
if (A.empty())
return {};
for (auto aa : A) {
TreeNode* tar = new TreeNode(aa);
while (tempM.size() > 0&& aa>tempM[tempM.size() -1]->val) {
tar->left = tempM[tempM.size() - 1];
tempM.pop_back();
}
if (tempM.size() != 0) {
tempM[tempM.size() - 1]->right = tar;
}
tempM.push_back(tar);
}
return tempM[0];
}
};
int main()
{
vector<int> aa = { 2, 5, 6, 0, 3, 1 };
Solution ss;
auto ans =ss.maxTree(aa);
//vector<vector<int>> ans(23, vector<int>());
getchar();
return 0;
}