[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.

时间: 2024-11-05 12:33:25

[Coding Made Simple] Count Number of Binary Tree Possible given Preorder Sequence的相关文章

leetcode day4 -- Binary Tree Postorder(Preorder) Traversal &amp;&amp; Edit Distance

 1.Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 分析:后续遍历

[Swift Weekly Contest 118]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal

Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this binary tree can be flipped by swapping the left child and the right child of that node. Consider the sequence of N values reported by a preorder traver

[LeetCode][JavaScript]Binary Tree Preorder Traversal

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? https://leetcod

Leetcode: Binary Tree Preorder Traversal(二叉树非递归前序遍历)

题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 二叉树的前序遍历 先看递归的写法(C++): /** * Definition f

【LeetCode】105 &amp; 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and Postorder' (Problem 106), u need build the binary tree. Input: 105. Preorder & Inorder traversal 106. Inorder & Postorder traversal output: A binar

Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. Given: 1 / 2 3 / 4 5 return [1,2,4,5,3]. Thinking: For this problem, you need to think about using recursive or non-recursive methods. As recursive method, we should think the o

LintCode: Binary Tree Preorder Traversal

C++,递归 1 /** 2 * Definition of TreeNode: 3 * class TreeNode { 4 * public: 5 * int val; 6 * TreeNode *left, *right; 7 * TreeNode(int val) { 8 * this->val = val; 9 * this->left = this->right = NULL; 10 * } 11 * } 12 */ 13 14 class Solution { 15 pub

144.Binary Tree Preorder Traversal(非递归前序遍历)

Given a binary tree, return the preorder traversal of itsnodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution istrivial, could you do it iteratively? HideTags Tree Stack #pragma once #include<ios

[C++]LeetCode: 95 Binary Tree Preorder Traversal (先序遍历)

题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? Answer 1: 递归法 思路:如果当前节点不为空,先把当前节点的值放入数组.从