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.

  • 相关阅读:
    $.each与$().each
    冒泡排序和用for循环画菱形
    windows10, 安装wamp无法启动服务的问题
    使用MySql数据库, 浏览器接收返回数据报错SyntaxError: unterminated string literal
    SWT, JFace必须的jar包和有可能会用到的jar
    easyui中tree使用simpleData的形式加载数据
    树形菜单的json字符串的拼接
    选择合适的String拼接方法(这篇博客是我抄的)
    hibernate进行多表联合查询
    操作文件和目录
  • 原文地址:https://www.cnblogs.com/lz87/p/7292292.html
Copyright © 2011-2022 走看看