当前位置:网站首页>剑指Offer 33.二叉搜索树的后序遍历序列
剑指Offer 33.二叉搜索树的后序遍历序列
2022-08-02 03:33:00 【HotRabbit.】
题目
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true
,否则返回 false
。假设输入的数组的任意两个数字都互不相同。
参考以下这颗二叉搜索树:
5
/ \
2 6
/ \
1 3
示例 1:
输入: [1,6,3,2,5]
输出: false
示例 2:
输入: [1,3,2,6,5]
输出: true
提示:
数组长度 <= 1000
Related Topics
- 栈
- 树
- 二叉搜索树
- 递归
- 二叉树
- 单调栈
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
题解
1.递归分治
终止条件: 当 i≥j ,说明此子树节点数量 ≤1 ,无需判别正确性,因此直接返回 tru**e ;
递推工作:
- 划分左右子树: 遍历后序遍历的 [i,j] 区间元素,寻找 第一个大于根节点 的节点,索引记为 m 。此时,可划分出左子树区间 [i,m−1] 、右子树区间 [m,j−1] 、根节点索引 j 。
- 判断是否为二叉搜索树:
- 左子树区间 [i,m−1] 内的所有节点都应 < postord**er[j] 。而第
1.划分左右子树
步骤已经保证左子树区间的正确性,因此只需要判断右子树区间即可。 - 右子树区间 [m,j−1] 内的所有节点都应 > postord**er[j] 。实现方式为遍历,当遇到 ≤postord**er[j] 的节点则跳出;则可通过 p=j 判断是否为二叉搜索树。
- 左子树区间 [i,m−1] 内的所有节点都应 < postord**er[j] 。而第
返回值: 所有子树都需正确才可判定正确,因此使用 与逻辑符 && 连接。
- *p*=*j* : 判断 此树 是否正确。
- ***rec*u*r*(*i*,*m*−1) : 判断 此树的左子树 是否正确。
- ***rec*u*r*(*m*,*j*−1) : 判断 此树的右子树 是否正确。
class Solution {
public boolean verifyPostorder(int[] postorder) {
return recur(postorder,0,postorder.length-1);
}
public boolean recur(int[] postorder,int i,int j){
if (i >= j){
return true;
}
int p = i;
while (postorder[p] < postorder[j]) p++;//找到第一个大于postorder[j]的数
int m = p;
while (postorder[p] > postorder[j]) p++;
return p == j && recur(postorder,i,m-1) && recur(postorder,m,j-1);
}
}
作者:jyd
链接:https://leetcode.cn/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/solution/mian-shi-ti-33-er-cha-sou-suo-shu-de-hou-xu-bian-6/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
2.辅助单调栈
后序遍历倒序:[ 根节点 | 右子树 | 左子树 ]
。类似 先序遍历的镜像 ,即先序遍历为 “根、左、右” 的顺序,而后序遍历的倒序为 “根、右、左” 顺序
class Solution {
public boolean verifyPostorder(int[] postorder) {
Stack<Integer> stack = new Stack<>();
int root = Integer.MAX_VALUE;
for(int i = postorder.length - 1; i >= 0; i--) {
if(postorder[i] > root) return false;
while(!stack.isEmpty() && stack.peek() > postorder[i])
root = stack.pop();
stack.add(postorder[i]);
}
return true;
}
}
作者:jyd
链接:https://leetcode.cn/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/solution/mian-shi-ti-33-er-cha-sou-suo-shu-de-hou-xu-bian-6/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
猜你喜欢
【面试必看】链表的常见笔试题
【Arduino connects SD card module to realize data reading and writing】
uniCloud use
开源日志库 [log4c] 使用
电子密码锁_毕设‘指导’
简单的RC滤波电路
Personal image bed construction based on Alibaba Cloud OSS+PicGo
【Popular Science Post】UART Interface Communication Protocol
【MQ-3 Alcohol Detector and Arduino Detect Alcohol】
字符串匹配(蛮力法+KMP)
随机推荐
振芯科技GM8285C:功能TTL转LVDS芯片简介
【面试必看】链表的常见笔试题
proteus数字电路仿真——入门实例
MIPI解决方案 ICN6202:MIPI DSI转LVDS转换芯片
path 修补文件命令
【详解】线程池及其自定义线程池的实现
所有子字符串中的元音 —— LeetCode - 2063
【nRF24L01 connects with Arduino to realize wireless communication】
[Arduino connects the clock module to display the time on LCD1602]
[Arduino uses a rotary encoder module]
【LeetCode】合并
WebApp 在线编程成趋势:如何在 iPad、Matepad 上编程?
【plang 1.4.4】编写贪吃蛇脚本
将ORCAD原理图导入allegro中进行PCB设计
引擎开发日志:重构骨骼动画系统
uniCloud use
Anaconda(Jupyter)里发现不能识别自己的GPU该怎么办?
ICN6211:MIPI DSI转RGB视频转换芯片方案介绍 看完涨知识了呢
R语言 —— 多元线性回归
【详解】优先级队列的底层实现