[Swift]LeetCode894. 全部可能的滿二叉樹 | All Possible Full Binary Trees

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-gwdxuuuh-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

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

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

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

Example 1:app

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:ide

  • 1 <= N <= 20

滿二叉樹是一類二叉樹,其中每一個結點剛好有 0 或 2 個子結點。spa

返回包含 N 個結點的全部可能滿二叉樹的列表。 答案的每一個元素都是一個可能樹的根結點。code

答案中每一個樹的每一個結點都必須有 node.val=0htm

你能夠按任何順序返回樹的最終列表。

 

示例:

輸入:7
輸出:[[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]]
解釋:

 

提示:

  • 1 <= N <= 20

116ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     var countToTrees: [Int: [TreeNode]] = [
16         1: [TreeNode(0)]
17     ]
18 
19     func allPossibleFBT(_ N: Int) -> [TreeNode?] {
20         if N % 2 == 0 || N < 0 {
21             return []
22         } else if let trees = countToTrees[N] {
23             return trees
24         }
25         
26         var fbts = [TreeNode]()
27         
28         let remaining = N - 1
29         var leftCount = 1
30         while leftCount < N - 1 {
31             let leftFbts = allPossibleFBT(leftCount)
32             let rightFbts = allPossibleFBT(remaining - leftCount)
33             
34             for lfbt in leftFbts {
35                 for rfbt in rightFbts {
36                     let root = TreeNode(0)
37                     root.left = lfbt
38                     root.right = rfbt
39                     fbts.append(root)
40                 }
41             }
42             
43             leftCount += 2
44         }
45         
46         countToTrees[N] = fbts
47         return fbts
48     }
49 }

120ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     var dict: Dictionary<Int,[TreeNode]> = [:]
16     func allPossibleFBT(_ N: Int) -> [TreeNode?] {
17         if N % 2 == 0 {
18             return []
19         }
20         
21         return expand(N - 1)
22     }
23     
24     private func expand(_ N: Int) -> [TreeNode] {
25         var result: [TreeNode] = []
26         if N == 0 {
27             return [TreeNode(0)]
28         } else if N == 2 {
29             let newNode = TreeNode(0)
30             newNode.left = TreeNode(0)
31             newNode.right = TreeNode(0)
32             result.append(newNode)
33             return result
34         } else {
35             if let nodes = dict[N] {
36                 return nodes
37             }
38             for i in 0...(N - 2)/2 {
39                 let left = i * 2
40                 let right = (N - 2) - left
41                 let leftNodes = expand(left)
42                 let rightNodes = expand(right)
43                 for lNode in expand(left) {
44                     for rNode in expand(right) {
45                         let newNode = expand(2)[0]
46                         newNode.left = lNode
47                         newNode.right = rNode
48                         result.append(newNode)
49                     }
50                 }
51             }
52             dict[N] = result
53             return result
54         }
55     }
56 }

136ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func allPossibleFBT(_ N: Int) -> [TreeNode?] {
16     if N == 1 {
17         return [TreeNode(0)]
18     } else if N < 1 || N % 2 == 0 {
19         return []
20     }
21     
22     var arr: [TreeNode] = []
23     for i in stride(from: 1, to: N-1, by: 2) {
24         let lArr = allPossibleFBT(i)
25         let rArr = allPossibleFBT(N-i-1)
26         
27         for j in 0..<lArr.count {
28             for k in 0..<rArr.count {
29                 let t = TreeNode(0)
30                 t.left = lArr[j]
31                 t.right = rArr[k]
32                 arr.append(t)
33             }
34         }
35     }    
36     return arr
37     }
38 }
Runtime: 152 ms
Memory Usage: 23.9 MB
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     var cache:[Int:[TreeNode?]] = [Int:[TreeNode?]]()
16     func allPossibleFBT(_ N: Int) -> [TreeNode?] {
17         var N = N
18         var res:[TreeNode?] = [TreeNode?]()
19         if N % 2==0 {return res}
20         if cache[N] != nil {return []}
21         if N == 1
22         {
23             res.append(TreeNode(0))
24             return res
25         }
26         N -= 1
27         for i in stride(from:1,to:N,by:2)
28         {
29             var left:[TreeNode?] = allPossibleFBT(i)
30             var right:[TreeNode?] = allPossibleFBT(N - i)
31             for nl in left
32             {
33                 for nr in right
34                 {
35                     var cur:TreeNode? = TreeNode(0)
36                     cur?.left = nl
37                     cur?.right = nr
38                     res.append(cur)
39                 }
40             }
41         }
42         cache[N] = res
43         return res
44     }
45 }
相關文章
相關標籤/搜索