Given an array with no repeating elements, how to build a tree

For the number being processed, find the two closest numbers to the element on the left and right sides that are larger than it, and take the smaller number as the root node of the current number

The process of building a tree is implemented with 2 tables, a left table and a right table

for example

3 4 5 1 2

right table

key  value

3        4

4         5

5        null

1         2

2        null

The establishment of the table uses the stack structure, keeping the elements in the stack at the bottom and the decimal at the top

Press in 3 

Push 4 Pop 3 Record 3 4

Push 5 Pop 4 Record 4 5

Press in 1 does not bounce

Push 2 Pop 1 Record 1 2

-------- pop 2 records 2 null

-------- pop 5 records 5 null 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325667733&siteId=291194637