LeetCode(剑指 Offer)- 33. 二叉搜索树的后序遍历序列

简介: LeetCode(剑指 Offer)- 33. 二叉搜索树的后序遍历序列

题目链接:点击打开链接

题目大意:

解题思路

image.png

相关企业

  • 微软(Microsoft)
  • Facebook
  • 字节跳动

AC 代码

  • Java
classSolution {
publicbooleanverifyPostorder(int[] postorder) {
returnrecur(postorder, 0, postorder.length-1);
    }
booleanrecur(int[] postorder, inti, intj) {
if(i>=j) returntrue;
intp=i;
while(postorder[p] <postorder[j]) p++;
intm=p;
while(postorder[p] >postorder[j]) p++;
returnp==j&&recur(postorder, i, m-1) &&recur(postorder, m, j-1);
    }
}
  • C++
classSolution {
public:
boolverifyPostorder(vector<int>&postorder) {
returnrecur(postorder, 0, postorder.size() -1);
    }
private:
boolrecur(vector<int>&postorder, inti, intj) {
if(i>=j) returntrue;
intp=i;
while(postorder[p] <postorder[j]) p++;
intm=p;
while(postorder[p] >postorder[j]) p++;
returnp==j&&recur(postorder, i, m-1) &&recur(postorder, m, j-1);
    }
};
目录
相关文章
|
1月前
|
存储 算法
《LeetCode》—— 摆动序列
《LeetCode》—— 摆动序列
|
2天前
|
算法 定位技术
【leetcode】剑指 Offer II 105. 岛屿的最大面积-【深度优先DFS】
【leetcode】剑指 Offer II 105. 岛屿的最大面积-【深度优先DFS】
10 0
|
2天前
|
算法
【力扣】94. 二叉树的中序遍历、144. 二叉树的前序遍历、145. 二叉树的后序遍历
【力扣】94. 二叉树的中序遍历、144. 二叉树的前序遍历、145. 二叉树的后序遍历
Leetcode1038. 从二叉搜索树到更大和树(每日一题)
Leetcode1038. 从二叉搜索树到更大和树(每日一题)
|
1月前
|
存储
力扣187 重复DNA序列
力扣187 重复DNA序列
|
1月前
|
存储 Serverless 索引
二叉树的前序遍历 、二叉树的最大深度、平衡二叉树、二叉树遍历【LeetCode刷题日志】
二叉树的前序遍历 、二叉树的最大深度、平衡二叉树、二叉树遍历【LeetCode刷题日志】
|
2月前
|
Java
LeetCode题解-二叉搜索树中第K小的元素-Java
LeetCode题解-二叉搜索树中第K小的元素-Java
12 0
|
2月前
|
存储
LeetCode题94,44,145,二叉树的前中后序遍历,非递归
LeetCode题94,44,145,二叉树的前中后序遍历,非递归
32 0
|
3月前
代码随想录 Day44 动规12 LeetCode T300 最长递增子序列 T674 最长连续递增序列 T718 最长重复子数组
代码随想录 Day44 动规12 LeetCode T300 最长递增子序列 T674 最长连续递增序列 T718 最长重复子数组
42 0
|
3月前
|
算法
代码随想录Day34 LeetCode T343整数拆分 T96 不同的二叉搜索树
代码随想录Day34 LeetCode T343整数拆分 T96 不同的二叉搜索树
30 0