当前位置:网站首页>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>
边栏推荐
- Five best software architecture patterns that architects must understand
- 通过反射执行任意类的任意方法
- Interesting interview questions
- js1day(輸入輸出語法,數據類型,數據類型轉換,var和let區別)
- Redis introduction, scenario and data type
- C modifier
- VIM super practical guide collection of this one is enough
- Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
- JDBC 预防sql注入问题与解决方法[PreparedStatement]
- Js1day (syntaxe d'entrée / sortie, type de données, conversion de type de données, Var et let différenciés)
猜你喜欢
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution
Redis bloom filter
8 examples of using date commands
Interval DP acwing 282 Stone merging
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
2.7 binary tree, post order traversal - [FBI tree]
Linear DP acwing 899 Edit distance
浏览器node事件循环
Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)
Package management tools
随机推荐
Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
[ybtoj advanced training guidance] judgment overflow [error]
8A Synchronous Step-Down regulator tps568230rjer_ Specification information
Interesting interview questions
Redis sentinel mechanism and configuration
JDBC prevent SQL injection problems and solutions [preparedstatement]
Linear DP acwing 899 Edit distance
JSON序列化 与 解析
Redis bloom filter
线性DP AcWing 896. 最长上升子序列 II
获取文件版权信息
Redis avalanche, penetration, breakdown
Variable, "+" sign, data type
The UVM Primer——Chapter2: A Conventional Testbench for the TinyALU
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
Hash table acwing 840 Simulated hash table
std::vector批量导入快速去重方法
Dijkstra AcWing 850. Dijkstra finding the shortest circuit II
Redis introduction, scenario and data type