Leetcode Summary--Two fork find Tree Chapter

Source: Internet
Author: User
This summary mainly introduces a more common data structure--two fork lookup tree. Binary lookup tree is not only a tree, but also with a special order nature, so the way to study more and more flexible, belong to the interview subject of frequent guests. Leetcode in the two-fork lookup tree in the following topics:
Validate Binary Search Tree
Recover Binary Search Tree
Unique Binary Search Trees
Unique Binary Search Trees II
Convert Sorted Array to Binary Search tree
Convert Sorted List to Binary Search tree

First look at the most basic Validate Binary search tree, which is to determine whether a tree is binary lookup trees. The simpler and clearer method is to use the binary lookup tree in order to traverse the ordered nature, as long as the tree in a sequence traversal, and the nodes are all satisfied with the order, the realization is to maintain a precursor node, each judge the precursor node than the current node is smaller. Another method is based on the definition of a binary lookup tree, the node is guaranteed to satisfy its left subtree each node is smaller than the current node value, each node of the right subtree is larger than the current node value, the realization is for each node to save the left and right bounds, and then make a recursive judgment about the boundary can not be violated.

Recover Binary Search Tree This topic still uses the binary to find the main nature of the trees, that is, the sequence traversal is orderly. If one of the elements is replaced, it means that the sequence traversal is bound to occur in a breach of order. The main consideration is the number of times a breach occurs. Here are two things:
(1) If the sequence traversal of the adjacent two elements have been replaced, it is easy to think of only one violation of the situation, only to the two nodes recorded the last Exchange value can be;
(2) If the two elements are not adjacent to be replaced, there will be two times in reverse order, then the elements that need to be replaced should be the first time in reverse order elements before, and the second reverse of the elements behind.

Unique Binary Search Trees This problem requires a viable two fork to find the number of trees, in fact, the binary lookup tree can be arbitrarily taken root, as long as the order to meet the ordered traversal of the requirements can be. From the point of view of dealing with child problems, select a node as the root, the node cut into the left and right subtree, with this node as the root of the number of viable binary trees is the left and right subtree the number of viable binary tree, so the total number is to all nodes as the root of the feasible results. This is actually a Cattleya number model, so follow the formula to achieve it. The Unique Binary Search Trees II can not use the Cattleya number, because all the results required, so still have to walk the process of recursive traversal, and then the spanning tree to the tree to connect.

Convert Sorted array to Binary search and convert Sorted List to Binary search trees are the construction of binary lookup tree, which is constructed for two kinds of different data structure arrays and linked lists. In fact, the method is the same, is recursive to the window loop, and then use the middle node as the current root, and then recursively generate the left and right subtree. The construction of a linked list has to be a little bit around, because you go to the first node through the middle-order traversal, and then the progressive list.

This summary mainly introduces the problem of binary lookup tree in Leetcode, binary lookup tree because it is the basic data structure plus the availability of an orderly nature, or in the interview is quite common, for the nature of understanding to be profound, to achieve proficiency in Kazakhstan.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.