日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

173. 二叉搜索树迭代器/94. 二叉树的中序遍历/145. 二叉树的后序遍历/98. 验证二叉搜索树

發布時間:2025/3/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 173. 二叉搜索树迭代器/94. 二叉树的中序遍历/145. 二叉树的后序遍历/98. 验证二叉搜索树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2020-05-12

1.題目描述

二叉搜索樹迭代器

2.題解

對于二叉搜索樹而言,進行中序遍歷就可以得到其有序序列,我們可以先對樹進行遍歷,將結果保存在 vector中,然后進行計算即可。

3.代碼

173

/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/ class BSTIterator { public:BSTIterator(TreeNode* root) {TreeNode* p=root;while (p||!mystack.empty()){while (p){mystack.push(p);p=p->left;}if (!mystack.empty()){myvector.push_back(mystack.top()->val);p=mystack.top();mystack.pop();p=p->right;}}index=0;}/** @return the next smallest number */int next() {return myvector[index++];}/** @return whether we have a next smallest number */bool hasNext() {if (index>=myvector.size()) return false;return true;}stack<TreeNode*> mystack;vector<int> myvector;int index; };/*** Your BSTIterator object will be instantiated and called as such:* BSTIterator* obj = new BSTIterator(root);* int param_1 = obj->next();* bool param_2 = obj->hasNext();*/

94

/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/ class Solution { public:vector<int> inorderTraversal(TreeNode* root) {vector<int> myvector;stack<TreeNode*> mystack;TreeNode* p=root;while (p||!mystack.empty()){while(p){mystack.push(p);p=p->left;}if (!mystack.empty()){p=mystack.top();mystack.pop();myvector.push_back(p->val);p=p->right;}}return myvector;} };

145

/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/ class Solution { public:vector<int> postorderTraversal(TreeNode* root) {vector<int> myvector;stack<TreeNode*> mystack;TreeNode* p=root;TreeNode* pre=p;while (p||!mystack.empty()){while (p){mystack.push(p);p=p->left;}TreeNode* top=mystack.top();if (!top->right||pre==top->right){myvector.push_back(top->val);mystack.pop();pre=top;}else{p=top->right;}}return myvector;} };

98

一開始將pre設置成最小的int,沒想到測試樣例中竟然剛好出現了[-2147483648],只能使用long long /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/ class Solution { public:bool isValidBST(TreeNode* root) {// 對其進行中序遍歷,看其是否是遞增的即可if (!root) return true; // 根節點為空stack<TreeNode*> mystack;TreeNode* p=root;long long pre=(long long)INT_MIN-1; // 保留其中序遍歷的上一個數,看是否滿足遞增的條件while (p||!mystack.empty()){while (p){mystack.push(p);p=p->left;}p=mystack.top();mystack.pop();if (p->val<=pre) return false;pre=p->val;p=p->right;}return true;} }; 新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!

總結

以上是生活随笔為你收集整理的173. 二叉搜索树迭代器/94. 二叉树的中序遍历/145. 二叉树的后序遍历/98. 验证二叉搜索树的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。