lintcode 126 堆棧用法

圖片.png
  • 思路 以及算法過程:


    圖片.png
  • 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;
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)載自VR設(shè)計云課堂[http://www.itdecent.cn/u/c7ffdc4b379e]Unity S...
    水月凡閱讀 1,173評論 0 0
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標準。 注意:講述HT...
    kismetajun閱讀 28,815評論 1 45
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,315評論 4 61
  • 前端html代碼 前端js代碼 前端css代碼 后端php返回json
    gogocheng閱讀 1,867評論 1 1
  • 剛剛下火車到家,媽媽電話“親愛的明天農(nóng)歷23,祝我親愛的姑娘生日快樂...最近怎么樣?工作?生活?感情?”哎呀,感...
    肉都給我吃閱讀 292評論 0 0

友情鏈接更多精彩內(nèi)容