zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    Bash快捷键
    Java Web学习笔记--JSP for循环
    Python学习笔记--简介
    Java学习笔记-数组
    JavaScript学习笔记一
    Java数组
    MongoDB学习---安装配置
    Java Web学习笔记-Servlet不是线程安全的
    Java Web学习笔记-重定向Redirect
    获取汇率的BAPI
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11096312.html
Copyright © 2011-2022 走看看