

JZ-023-二叉搜索树的后序遍历序列
source link: https://segmentfault.com/a/1190000041103032
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JZ-023-二叉搜索树的后序遍历序列
二叉搜索树的后序遍历序列
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则返回true,否则返回false。假设输入的数组的任意两个数字都互不相同。
题目链接: 二叉搜索树的后序遍历序列
/** * 标题:二叉搜索树的后序遍历序列 * 题目描述 * 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则返回true,否则返回false。假设输入的数组的任意两个数字都互不相同。 * 题目链接: * https://www.nowcoder.com/practice/a861533d45854474ac791d90e447bafd?tpId=13&&tqId=11176&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking */ public class Jz23 { public boolean verifySquenceOfBST(int[] sequence) { if (sequence == null || sequence.length == 0) { return false; } return verify(sequence, 0, sequence.length - 1); } /** * 递归法 * * @param sequence * @param first * @param last * @return */ private boolean verify(int[] sequence, int first, int last) { if (last - first <= 1) { return true; } int rootVal = sequence[last]; int cutIndex = first; while (cutIndex < last && sequence[cutIndex] <= rootVal) { cutIndex++; } for (int i = cutIndex; i < last; i++) { if (sequence[i] < rootVal) { return false; } } return verify(sequence, first, cutIndex - 1) && verify(sequence, cutIndex, last - 1); } public static void main(String[] args) { } }
【每日寄语】 纵有疾风来,人生不言弃。风乍起,合当奋意向此生。
Recommend
-
32
本题主要在于考察对二叉搜索树和后序遍历的理解。 原题 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true ,否则返回 false 。假设输入的数组的任意两...
-
23
ARTS ARTS 是陈浩(网名左耳朵耗子)在极客时间专栏里发起的一个活动,目的是通过分享的方式来坚持学习。 每人每周写一个 ARTS:Algorithm 是一道算法题,Review 是读一篇英文文章,Technique/Tips 是分享一个小技术...
-
14
二叉树非递归后序遍历2014-06-16翻到自己以前考研时发过的帖子,真心觉得那时候比现在牛B多了...网上传的很多二叉树非递归遍历的算法,都是在结点中加了一个标记的,第二次访问到...
-
7
美团面试官:你对后序遍历一无所知 培养框架思维,真正爱上算法!关注公众号查看更新文章👆 相关推荐: 读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目:
-
14
LeetCode 第 145 号问题:二叉树的后序遍历-程序员小吴 当前位置:程序员小吴 > LeetCodeAnimation > LeetCode 第 145 号问题:二叉树的后序遍...
-
10
LeetCode-145-二叉树的后序遍历发布于 今天 23:54 二叉树的后序遍历题目描述:给定一个二叉树,返回它的 后序 遍历。示例说...
-
10
1 题目描述给定一个 n 叉树的根节点 root ,返回 其节点值的 后序遍历 。n叉树在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔。
-
10
go 二叉树的非递归遍历 code TreeNode type TreeNode struct { Val int Left *TreeNode Right *TreeNode } 先序遍历 中左右 func preorderTravers...
-
10
二叉树链式结构 前一篇博客介绍了二叉树的顺序结构,是通数组来存储的,这里我们通过创建链式结构来存储,在堆上申请空间,结构如下: template <class DateType> struct BinaryTreeNode { DateT...
-
9
二叉树的遍历 主要的三种遍历方式 二叉树主要的遍历方式有前序遍历、中序遍历和后序遍历。 (1)前序遍历:根节点-->左子树-->右子树
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK