LeetCode 894. All Possible Full Binary Trees

原题链接在这里:https://leetcode.com/problems/all-possible-full-binary-trees/

题目:

full binary tree is a binary tree where each node has exactly 0 or 2 children.

Return a list of all possible full binary trees with N nodes.  Each element of the answer is the root node of one possible tree.

Each node of each tree in the answer must have node.val = 0.

You may return the final list of trees in any order.

 

Example 1:

Input: 7
Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
Explanation:

 

Note:

  • 1 <= N <= 20

题解:

If we mark node from 0 to N-1.

Only the odd index node, like 1, 3, 5 could be root. Since count of nodes on left is odd and count of node on right is odd.

Even number of nodes can't form a full binary tree.

Could use recursive call to get a list of possible left subtrees, and a list of possible right subtrees.

For each of them, append it back to root, and add to res.

We could use cache to memorize results of N. Then no need to recalculate. 

Also it would be bebetter to deep clone the subtree, or in the future if one node is changed, all its reference would be changed too.

Time Complexity: O(n^3). T(N) = T(1)*T(N-2) + T(3)*T(N-4) + ... + T(N-2)*T(1). With Cache, when calculate T(N-2), all number smaller than N-2 is calculated and put into cache. Then for the rest, get T(i) from cache needs O(1) time. There could be O(N) results for subtrees, size of list could be O(N). Totoally, equation length is N/2, Then T(N) = N/2 * (N*N) = O(N^3).

Space: O(N^3). cache count is O(N). For each value, list size could be O(N). Within list, each tree could be O(N). Thus, it takes O(N^3) space.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     HashMap<Integer, List<TreeNode>> hm = new HashMap<Integer, List<TreeNode>>();
12     
13     public List<TreeNode> allPossibleFBT(int N) {
14         if(hm.containsKey(N)){
15             return hm.get(N);
16         }
17         
18         List<TreeNode> res = new ArrayList<TreeNode>();
19         if(N<1 || N%2 == 0){
20             hm.put(N, res);
21             return res;
22         }
23         
24         if(N == 1){
25             res.add(new TreeNode(0));
26             hm.put(N, res);
27             return res;
28         }
29         
30         for(int i = 1; i<N; i+=2){
31             List<TreeNode> left = allPossibleFBT(i);
32             List<TreeNode> right = allPossibleFBT(N-1-i);
33 
34             for(TreeNode l : left){
35                 for(TreeNode r : right){
36                     TreeNode root  = new TreeNode(0);
37                     root.left = deepClone(l);
38                     root.right = deepClone(r);
39                     res.add(root);
40                 }
41             }
42         }
43         
44         hm.put(N, res);
45         return res;
46     }
47     
48     private TreeNode deepClone(TreeNode root){
49         if(root == null){
50             return root;
51         }
52         
53         TreeNode copy = new TreeNode(root.val);
54         copy.left = deepClone(root.left);
55         copy.right = deepClone(root.right);
56         return copy;
57     }
58 }

类似Unique Binary Search Trees II.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11096312.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值