zoukankan      html  css  js  c++  java
  • [Coding Made Simple] Count Number of Binary Tree Possible given Preorder Sequence

    Count Number of Binary Tree Possible given Preorder Sequence. 

    For example, given preorder sequence of {10, 11, 9, 12, 13, 14}, the total possible number of binary trees is 42.

    Solution 1. Recursion

    Since we are only trying to find possbile binary trees, the actual values of the given preorder sequence do not matter, 

    the length of the sequence matters.

    For a given sequence of length n, the first number is always the root node. The rest of n - 1 nodes can be assigned to form the left

    and right subtree as follows. All nodes for one subtree must be consecutive in the sequence based on the preorder property. If a 

    split point is fixed, the first half nodes form left subtrees and the second half nodes form right subtrees.

    0 nodes for left subtree, n - 1 nodes for right subtrees;

    1 node for left subtree, n - 2 nodes for right subtree;

    2 nodes for left subtree, n - 3 nodes for right subtree;

    .........

    n - 1 nodes for left subtree, 0 node for right subtree.

    Again, this recursive algorithm suffers from overlapping subproblems that are evaluated over and over. Dynamic programming should be used to avoid this redundancy.

     1 public class NumberOfBT {
     2     public int getNumberOfBt(int[] preOrder) {
     3         if(preOrder == null) {
     4             return 1;
     5         }
     6         return recursiveHelper(preOrder.length);
     7     }
     8     private int recursiveHelper(int len) {
     9         if(len <= 1) {
    10             return 1;
    11         }
    12         int cnt = 0;
    13         for(int leftLen = 0; leftLen < len; leftLen++) {
    14             cnt += recursiveHelper(leftLen) * recursiveHelper(len - 1 -leftLen);
    15         }
    16         return cnt;
    17     }
    18 }

    Solution 2. Dynamic Programming

    State: T[i]: the total number of binary trees when there are i nodes in the preorder sequence.

     1 public int getNumberOfBt(int[] preOrder) {
     2     if(preOrder == null) {
     3         return 1;
     4     }
     5     int[] T = new int[preOrder.length + 1];
     6     T[0] = 1; 
     7     for(int i = 1; i <= preOrder.length; i++) {
     8         for(int j = 0; j < i; j++) {
     9             T[i] += T[j] * T[i - 1 - j];
    10         }
    11     }
    12     return T[preOrder.length];
    13 }

    Follow up question: If the problem is changed to Count Number of Binary Search Tree Possible given Preorder Sequence, do we still need to do any calculations?

    Answer: if a given preorder sequence represents a binary search tree, then it uniquely determines 1 bst. No calculation is needed.

  • 相关阅读:
    vue 底层面试题
    js第二阶段的面试题
    vue新一轮的面试题
    vue3面试题
    day_33:后端day04Django框架中的视图和请求、响应
    day_37:后端day08Django框架前后端不分离模式实现项目管理系统(增删差改)
    day_36:后端day07Django框架中的ORM数据库操作二
    【漏洞复现】ThinkAdmin v5和v6 未授权列目录任意文件读取(CVE202025540)
    【超详细】安全测试===sqlmap使用心得(零)
    【最新】绕过Outlook 拦截钓鱼链接方式
  • 原文地址:https://www.cnblogs.com/lz87/p/7292292.html
Copyright © 2011-2022 走看看