class Solution {
public:
bool verifyTreeOrder(vector& postorder) {
return f(postorder,0,postorder.size()-1);
}
private:
bool f(vector &postorder, int i, int j){
if(i>=j) return true;
int p =i ;
while(postorder[p] < postorder[j]) p++;//找到第一个大于根的位置
int m =p;
while(postorder[p] > postorder[j]) p++;//继续往后遍历
return pj&&f(postorder,i,m-1)&& f(postorder,m,j-1);//递归左右子树。
}
};
java
class Solution{
public boolean verifyTreeOrder(int[] postorder){
return recur(postorder, 0, postorder.length – 1);
}
boolean recur(int[] postorder, int i, int j){
if(i >= j) return true;
int p = i;
while(postorder[p] < postorder[j]) p++;
int m = p;
while(postorder[p] > postorder[j]) p++;
return p j && recur(postorder, i, m – 1) && recur(postorder, m, j – 1);
}
}
提交
class Solution(object):
def verifyTreeOrder(self, postorder):
"""
:type postorder: List[int]
:rtype: bool
"""
if not postorder:
return True
def cur(postorder, l, r):
if l >= r:
return True
root = postorder[r]
p = l
for i in range(len(postorder)):
if postorder[p] < root:
p = i
for j in range(p,r):
if postorder[j] < root:
return False
return cur(postorder, l, p - 1) and cur(postorder, p, r - 1)
return cur(postorder, 0, len(postorder) - 1)
class Solution {
public:
bool verifyTreeOrder(vector& postorder) {
if (postorder.empty()) {
return true;
}
function<bool(int, int)> f = [&](int i, int j) {
if (i >= j) {
return true;
}
int root = postorder[j];
int p = i;
while (postorder[p] < root) {
++p;
}
for (int k = p; k < j; ++k) {
if (postorder[k] < root) {
return false;
}
}
return f(i, p - 1) && f(p, j - 1);
};
return f(0, postorder.size() - 1);
}
共以下 22 个回答
class Solution {
public boolean verifyTreeOrder(int[] postorder) {
if(postorder null)return true;
}
思路:思路与地哥在视频中讲解的一致,使用递归的方式判断后序遍历序列是否正确
“`c++& postorder) { & postorder, int left, int right) {
class Solution {
public:
bool verifyTreeOrder(vector
if (postorder.size() <= 1) return true; return f(postorder, 0, postorder.size()-1); } bool f(vector
if (left >= right) return true;
int idx = left;
while (idx < right && postorder[idx] <= postorder[right]) idx++; for (int i=idx; i
class Solution {& postorder) {
public:
bool verifyTreeOrder(vector
private: &postorder, int i, int j){
bool f(vector
if(i>=j) return true;
int p =i ;
while(postorder[p] < postorder[j]) p++;//找到第一个大于根的位置
int m =p;
while(postorder[p] > postorder[j]) p++;//继续往后遍历
return pj&&f(postorder,i,m-1)&& f(postorder,m,j-1);//递归左右子树。
}
};
java
class Solution{
public boolean verifyTreeOrder(int[] postorder){
return recur(postorder, 0, postorder.length – 1);
}
boolean recur(int[] postorder, int i, int j){
if(i >= j) return true;
int p = i;
while(postorder[p] < postorder[j]) p++;
int m = p;
while(postorder[p] > postorder[j]) p++;
return p j && recur(postorder, i, m – 1) && recur(postorder, m, j – 1);
}
}
提交
第一次打卡二叉搜索树
class Solution {& postorder) {
public:
bool verifyTreeOrder(vector
if (postorder.empty()) {
return true;
}
};
打卡:先找到比根节点大的第一个数,然后把数组拆分成左右子树,分别递归
day9第二题
时空复杂度O(N)
时空复杂度O(N)
public boolean verifyTreeOrder(int[] postorder) {
return recur(postorder,0,postorder.length-1);
}
boolean recur(int[] postorder,int i,int j){
if(i>=j) return true;
int p=i;
while(postorder[p] < postorder[j]) p++;//找到第一个比根节点大的数
int m = p;
while(postorder[p] > postorder[j]) p++;//确定右子树是否正确
return pj && recur(postorder,i,m-1) && recur(postorder,m,j-1);
}
思路:
根节点是后序遍历中的最后一个点,递归确定每个树节点的左子树是不是都小于根节点,再看右子树是不是都大于
打卡
https://leetcode.cn/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/solutions/2680161/yan-zheng-er-cha-sou-suo-shu-by-yukislab-4twz
打卡
class Solution {
public boolean verifyTreeOrder(int[] postorder) {
return rec(postorder,0,postorder.length-1);
}//递归写法
//时间复杂度On2
//空间复杂度On
“`C++& postorder) { & postorder, int left, int right)
class Solution {
public:
bool verifyTreeOrder(vector
return test(postorder, 0, postorder.size() – 1);
}
private:
bool test(vector
{
//递归结束条件
if(left >= right)
{
return true;
}
int cur = left;
int last = postorder[right];
while(postorder[cur] < last) { cur++; } //根节点 int root = cur; for(int i = cur; i < right; i++) { if(postorder[i] < last) { return false; } } return test(postorder, left, root - 1 ) && test(postorder, root, right - 1); } }; ```