当前位置:网站首页>The k-th node of the binary search tree [sword finger offer]
The k-th node of the binary search tree [sword finger offer]
2022-06-25 05:21:00 【Don't hit me, it hurts!】
subject

Answer key
- Law 1 :
Store the results of the middle order traversal of the binary search tree into the array , Take the first... Of the array k results .
function KthNode(pRoot, k)
{
if(!pRoot || k<1) return;
return gotoArr(pRoot)[k-1];
}
let res = [];
function gotoArr(root){
// The middle order traversal result is stored in the array
if(!root) return;
gotoArr(root.left);
res.push(root);
gotoArr(root.right);
return res;
}
Existing problems : You need to traverse all nodes , High time complexity
// 》》 Improved version 《《
let res = [];
let key; // flag: Determine whether to get the result
function KthNode(pRoot, k)
{
if(!pRoot || k<1) return;
key = k;
return gotoArr(pRoot)[k-1];
}
function gotoArr(root){
// The middle order traversal result is stored in the array
if(res.length === key) return; // Get the result , stop it
if(root.left) gotoArr(root.left);
res.push(root);
if(root.right) gotoArr(root.right);
return res;
}
- Law two :
In order to traverse the binary search tree , Search to k Node time , stop it .
function KthNode(root, k)
{
if(!root || k<1)return null;
let res = null;
// For tracking k value , Define the middle order traversal function inside
function traverse(root){
if(!root) return null;
if(root.left) res = traverse(root.left);
if(!res){
// No result found in the left subtree
if(k===1) res=root;
k--;
}
if(root.right) res = traverse(root.right);
return res;
}
return traverse(root);
}
Supplementary information :
Binary search tree :
a. Definition :
Binary search tree (Binary Search Tree), Also known as a binary search tree 、 Binary search tree 、 Ordered binary tree or ordered binary tree . Meet the following conditions :
- If its left subtree is not empty , The value of all nodes on the left subtree is less than its root node .
- If its right subtree is not empty , The value of all nodes on the right subtree is greater than its root node .
Its left 、 The right subtree is also a binary search tree . As shown in the figure below :
b. Binary search tree has efficient insertion 、 Delete 、 Query operation .
The time complexity of the average time is O(log n), The worst case scenario is O(n). Binary search tree is different from heap , It doesn't have to be a complete binary tree , The bottom layer is not easy to be directly represented by an array, so the linked list is used to realize the binary search tree .
c. Binary search process
The idea of binary search is 1946 in , Finding problems is a very important basic problem in computer , For an ordered sequence , To use binary search . If we're looking for an element , First look at the value in the middle of the array V And the size of the data you need to find , There are three situations :
- 1、 Equal to the data you want to find , Find it directly
- 2、 If less than V, In less than V Some groups continue to query
- 2、 If more than V, In more than V Some groups continue to query

Code :
var search = function(nums, target) {
let [start,end] = [0,nums.length-1];
let middle,midItem;// Intermediate element index and content
while(start<=end){
middle = Math.floor((start+end)/2);
midItem = nums[middle];
if(midItem === target){
return middle;
}
else if(midItem > target){
end = middle-1;
}
else{
start = middle+1;
}
}
return -1;
};
* Please quote from QW’s Blog!*
边栏推荐
- Integrate CDN to create the ultimate service experience for customers!
- UVA816 Abbott’s Revenge
- Keyboard key code value
- C language -- Sanzi chess
- Japanese fifty tone diagram
- Summary of SQL injection (I)
- The print area becomes smaller after epplus copies the template
- Two dimensional array and function call cases of C language
- Implementation of websocket long connection by workman under laravel
- Database low-end SQL query statement fragment
猜你喜欢

Attack and defense world web baby Web

API interface management setup -eolinker4.0

XSS (cross site script attack) summary (II)

Eyeshot Ultimate 2022 Crack By Xacker

滲透測試-提權專題
![H5 native player [learn video]](/img/51/83a200d0423b7274d1e981ec2ede2c.jpg)
H5 native player [learn video]

How to download and use Xiaobai one click reload on the official website

Student achievement management system based on SSH

Baidu ueeditor set toolbar initial value

hr竟主动给这位测试小姐姐涨工资,她是怎么做到的?
随机推荐
Tanhaoqiang C language practice
Penetration information collection steps (simplified version)
How to use the Magic pig system reinstallation master
What if win11 Bluetooth fails to connect? Solution of win11 Bluetooth unable to connect
A brief talk on media inquiry
Use js to simply implement the apply, call and bind methods
Database overview
2022.1.21 diary
Word of the Day
BUUCTF(web:1-50)
Conflict between v-mode and v-decorator in Ant Design
Visual studio 2022 interface beautification tutorial
Charles and iPhone capture
JSON Library Tutorial from scratch (II): parsing digital learning and sorting notes
渗透测试-提权专题
UVA816 Abbott’s Revenge
Deep analysis of recursion in quick sorting
Keyboard key code value
Duplicate symbols for architecture i386 clang
滲透測試-提權專題