table of Contents
Brushing leetcode has entered the intermediate problem stage, and found that the binary search tree has some problem-solving skills to follow, so it is recorded here.
1. Topic 1: Post-order traversal sequence of binary search tree (Sword refers to Offer 33)
Enter an integer array to determine whether the array is the result of a post-order traversal of a binary search tree. If it is, it returns true, otherwise it returns false. Assume that any two numbers in the input array are different from each other.
Refer to the following binary search tree:
5
/ \
2 6
/ \
1 3
Example:
输入: [1,6,3,2,5]
输出: false
solution:
To grasp the characteristics of post-order traversal and binary search tree post-order traversal :
1. Two characteristics of post-order traversal
①The last node is the root node
②Left subtree first, then right subtree, and the root node is placed last, last, last
2. The characteristics of binary search tree
The left subtree is smaller than the root node, and the right subtree is larger than the root node
3. The characteristics of the post-order traversal of the binary search tree
① The root node is at the end;
②The first number greater than the root node must be in the right subtree, and all values on its left in the array must be in the left subtree; all values on its right must be in the right subtree (except the last root node)
③In this way, the same judgment is made on the left and right subtrees, recursion + divide and conquer, and the judgment task can be completed.
Supplement: the in-order traversal feature of the binary search tree
The middle-order traversal result of the binary search tree is an ordered array arranged from small to large.