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.

  • 相关阅读:
    有效的完全平方数
    除自身以外数组的乘积leetcode
    三个数的最大乘积 leetcode
    不同的二叉搜索树
    fenzhi
    leetcode二分查找之大于给定元素的最小元素744. Find Smallest Letter Greater Than Target (Easy)
    leetcode二分查找之求开方
    总结U-Net网络及他的变体
    451. Sort Characters By Frequency (Medium) 按照字符出现次数对字符串排序(leetcode排序)
    记一次 PHP调用Java Webservice
  • 原文地址:https://www.cnblogs.com/lz87/p/7292292.html
Copyright © 2011-2022 走看看