Binary Search Tree
Binary Search Tree is one of the commonly used binary tree. Its definition is that all nodes on the left subtree are <= the node, and all nodes on the right subtree are >= the node.
BST’s value has orders. Therefore, most of its operations doesn’t need to search both right and left subtree, and its template is a bit different from the general traverse.
The blog is written based on labuladong git book.
void BST(TreeNode root, int target) {
if (root.val == target)
// 找到目标,做点什么
if (root.val < target)
BST(root.right, target);
if (root.val > target)
BST(root.left, target);
}
Also, BST’s in-order traverse is sorted. It is commonly used with in-order traversal.