当前位置:网站首页>Post order traversal sequence of 24 binary search tree of sword finger offer
Post order traversal sequence of 24 binary search tree of sword finger offer
2022-07-02 12:57:00 【Snail Internet】
Title Description
- Enter an array of integers , Determine whether the array is the result of the subsequent traversal of a binary search tree .
- If so, return ture, Otherwise return to false.
- Suppose that any two numbers of the input array are different from each other .
analysis
- Find the root node
- Ab initio traversal sequence , The first element larger than the root node is the starting point of the right subtree
- Determine whether the right subtree is larger than the root node , If not return false, if , Go to the next step
- Judge the left subtree and the right subtree according to the above rules , If the left and right subtrees can return true, Then the whole sequence is the post order traversal sequence of the binary search tree , return true
Code implementation
public boolean verifySquenceOfBST(int [] sequence) {
if(sequence == null || sequence.length == 0){
return false;
}
int len = sequence.length;
int root = sequence[len - 1];
int i = 0;
for(; i < len - 1; i++){
if(sequence[i] > root){
break;
}
}
int j = i;
for(;j < len - 1; j++){
if(sequence[j] < root){
return false;
}
}
boolean left = true;
boolean right = true;
if(i > 0){
left = verifySquenceOfBST(Arrays.copyOfRange(sequence, 0, i));
}
if(i < len - 1){
right = verifySquenceOfBST(Arrays.copyOfRange(sequence,i,len - 1));
}
return left && right;
}
<p></p>
边栏推荐
- 正确遍历EntryList方法
- Some sudden program ideas (modular processing)
- Rust search server, rust quick service finding tutorial
- spfa AcWing 852. spfa判断负环
- Linear DP acwing 895 Longest ascending subsequence
- 堆 AcWing 839. 模拟堆
- js4day(DOM开始:获取DOM元素内容,修改元素样式,修改表单元素属性,setInterval定时器,轮播图案例)
- js2day(又是i++和++i,if语句,三元运算符,switch、while语句,for循环语句)
- C operator
- [opencv] [image gradient]
猜你喜欢

C#修饰符

Apply lnk306gn-tl converter, non isolated power supply

线性DP AcWing 895. 最长上升子序列

js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)

包管理工具

js1day(輸入輸出語法,數據類型,數據類型轉換,var和let區別)

Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)

Counting class DP acwing 900 Integer partition

Browser node event loop

面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六
随机推荐
Rust search server, rust quick service finding tutorial
spfa AcWing 852. SPFA judgement negative ring
Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
[opencv learning] [template matching]
VLAN experiment
Package management tools
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
The UVM Primer——Chapter2: A Conventional Testbench for the TinyALU
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
Typora+docsify quick start
Enhance network security of kubernetes with cilium
获取文件版权信息
spfa AcWing 851. spfa求最短路
包管理工具
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
Structured data, semi-structured data and unstructured data
移动式布局(流式布局)
Linear DP acwing 902 Shortest editing distance
8A Synchronous Step-Down regulator tps568230rjer_ Specification information
Std:: vector batch import fast de duplication method