【数据结构】二叉树搜索树(二叉排序树)BST专题
作者:互联网
46. 二叉搜索树的后序遍历序列
class Solution {
public:
vector<int> seq;
bool verifySequenceOfBST(vector<int> sequence) {
seq = sequence;
return dfs(0, seq.size() - 1);
}
bool dfs(int l, int r)
{
if (l >= r) return true;
int root = seq[r]; // 后序遍历的最后一个元素为根节点
int k = l;
while (k < r && seq[k] < root) k ++ ;
for (int i = k; i < r; i ++ )
if (seq[i] < root)
return false;
return dfs(l, k - 1) && dfs(k, r - 1);
}
};
标签:return,seq,sequence,BST,dfs,二叉,int,二叉树,root 来源: https://www.cnblogs.com/Tshaxz/p/16652224.html