2
头图

Different binary search trees

Topic Description: to give you an integer n , seeking just the n -node and the node value from 1 to n different from each other binary search tree How many? Returns the number of binary search trees that satisfy the meaning of the question.

Binary Search Tree: also called a binary sort tree. It is either an empty tree or a binary tree with the following properties: If its left subtree is not empty, then all of the left subtrees The value of the node is less than the value of its root node; if its right subtree is not empty, the values of all nodes on the right subtree are greater than the value of its root node; its left and right subtrees They are also binary sort trees.

Please refer to LeetCode official website for example description.

Source: LeetCode
Link: https://leetcode-cn.com/problems/unique-binary-search-trees/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Solution one: recursive method
  • First, when n is 0, the result is an empty tree, and an empty list is returned directly.
  • When n is greater than 0, the recursive method is used to obtain the left and right subtrees respectively. The recursive process is as follows:

    • All nodes can be used as the root node, that is, all the values from 1 to n are traversed as the root node, and the current root node is i;
    • Then all the values on the left of i are called recursively as the left subtree of i;
    • All values on the right of i are called recursively as the right subtree of i;
    • Finally, the root node i and the corresponding left and right subtrees are assembled into a tree and placed in the result set.
  • Finally, the size value of the returned result set is the number of species of the binary search tree that meets the conditions.

description: This method refers to LeetCode-095-different binary search tree II , but it timed out when submitting.

Solution 1: Law
Finding the law shows that when the integer is n, the result of the binary search number is the sum of all the previous possible results.
import com.kaesar.leetcode.TreeNode;
import java.util.ArrayList;
import java.util.List;

public class LeetCode_096 {
    /**
     * 递归:该方法运行时超时了
     *
     * @param n
     * @return
     */
    public static int numTrees(int n) {
        // 当n为0的时候,是一棵空树
        if (n == 0) {
            return 1;
        }
        return generateTrees(1, n).size();
    }

    private static List<TreeNode> generateTrees(int start, int end) {
        List<TreeNode> allTrees = new ArrayList<>();
        if (start > end) {
            allTrees.add(null);
            return allTrees;
        }
        for (int i = start; i <= end; i++) {
            // 所有可能的左子树集合
            List<TreeNode> leftTrees = generateTrees(start, i - 1);

            // 所有可能的右子树集合
            List<TreeNode> rightTrees = generateTrees(i + 1, end);

            for (TreeNode leftTree : leftTrees) {
                for (TreeNode rightTree : rightTrees) {
                    TreeNode root = new TreeNode(i);
                    root.left = leftTree;
                    root.right = rightTree;
                    allTrees.add(root);
                }
            }
        }
        return allTrees;
    }
    
    public static int numTrees2(int n) {
        int[] result = new int[n + 1];
        result[0] = 1;
        result[1] = 1;
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                result[i] += result[j - 1] * result[i - j];
            }
        }
        return result[n];
    }

    public static void main(String[] args) {
        System.out.println(numTrees(3));
        System.out.println(numTrees2(3));
    }
}
[Daily Message] Young is the capital, but do not work hard is not valuable.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!