2
头图

Different Binary Search Trees II

Topic Description: to give you an integer n , you generate and return all the n -node and the node value from 1 to n with different mutually search binary tree . The answer can be any order

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, all 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-ii/
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 returned result set is all possible binary search trees.
import com.kaesar.leetcode.TreeNode;
import java.util.ArrayList;
import java.util.List;

public class LeetCode_095 {
    /**
     * 递归
     *
     * @param n
     * @return
     */
    public static List<TreeNode> generateTrees(int n) {
        // 当n为0的时候,是一棵空树
        if (n == 0) {
            return new ArrayList<>();
        }
        return generateTrees(1, n);
    }

    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 void main(String[] args) {
        for (TreeNode generateTree : generateTrees(3)) {
            generateTree.print();
            System.out.println();
        }
    }
}
[Daily Message] , the young man's head is white, empty and sad!

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

玉树临风,仙姿佚貌!