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.

  • 相关阅读:
    Highcharts 连续的堆积面积图
    jQuery--each遍历使用方法
    C# 常用小技巧
    JSON对象遍历方法
    T-SQL生成X个不重复的Y位长度的随机数
    SQLServer如何快速生成100万条不重复的随机8位数字
    ftp和http断点续传及下载的Delphi实现
    Delphi与Windows 7下的用户账户控制(UAC)机制
    Win7/Win10下的进程操作
    运行Delphi 2007 IDE提示无法打开"EditorLineEnds.ttr"文件
  • 原文地址:https://www.cnblogs.com/lz87/p/7292292.html
Copyright © 2011-2022 走看看