[Swift]LeetCode894. 所有可能的满二叉树 | All Possible Full Binary Trees

A 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


满二叉树是一类二叉树,其中每个结点恰好有 0 或 2 个子结点。

返回包含 N 个结点的所有可能满二叉树的列表。 答案的每个元素都是一个可能树的根结点。

答案中每个树的每个结点都必须有 node.val=0

你可以按任何顺序返回树的最终列表。

示例:

输入: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 }

原文地址:https://www.cnblogs.com/strengthen/p/10605621.html

时间: 2024-10-13 23:32:12

[Swift]LeetCode894. 所有可能的满二叉树 | All Possible Full Binary Trees的相关文章

leetcode 894. 所有可能的满二叉树(All Possible Full Binary Trees)

题目 满二叉树是一类二叉树,其中每个结点恰好有 0 或 2 个子结点. 返回包含 N 个结点的所有可能满二叉树的列表. 答案的每个元素都是一个可能树的根结点. 答案中每个树的每个结点都必须有 node.val=0. 你可以按任何顺序返回树的最终列表. 示例: 输入: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

代码赏析——满二叉树

/* 标题:锦标赛 如果要在n个数据中挑选出第一大和第二大的数据 (要求输出数据所在位置和值) 使用什么方法比较的次数最少? 我们可以从体育锦标赛中受到启发. 8个选手的锦标赛,先两两捉对比拼,淘汰一半. 优胜者再两两比拼...直到决出第一名. 第一名输出后,只要对黄色标示的位置重新比赛即可. 下面的代码实现了这个算法(假设数据中没有相同值). 代码中需要用一个数组来表示图中的树 (注意,这是个满二叉树,不足需要补齐). 它不是存储数据本身,而是存储了数据的下标. 第一个数据输出后,它所在的位置

完全二叉树、理想二叉树满二叉树

完全二叉树(Complete Binary Tree): 设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第h层所有的结点都连续集中在最左边,这就是完全二叉树. 理想二叉树(Perfect Binary Tree): 除最后一层无任何子节点外,每一层上的所有结点都有两个子结点的树称为理想二叉树.高度为h(从0开始算起)且包含2^(h+1)-1个节点的 二叉树是理想二叉树. 满二叉树(Full Binary Tree): 在国内实际上指的是上面提到的理想二叉树

BZOJ 2111 Perm 排列计数(满二叉树)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2111 题意:求1到n有多少种排列满足:A[i]>A[i/2](2<=i<=n). 思路:形式类似二叉树.建模之后其实就是n个节点的不同的满二叉树有多少种?用f[i]表示i个节点的满二叉树个数,则f[n]=f[L]*f[R]*C(n-1,L).其中L和R对于确定的n来说是确定的.比如n=10时,左右子树分别有6.3个点. i64 a[N],n,p,f[N]; void init(

如何判定一颗树是完全二叉树和满二叉树

满二叉树:一颗深度为k且有2^k-1个节点的二叉树称为满二叉树: 完全二叉树:对满二叉树的结点进行连续编号,约定编号从根结点起,自上而下,自左至右.深度为k的,有n个结点的二叉树,当且仅当其每一个结点都与深度为k的满二叉树编号从1至n的结点对应时,称为完全二叉树.如图所示: 1. 判定完全二叉树.判定一棵树是不是完全二叉树的思路是广度遍历该二叉树,当出现NULL值时停止遍历,如果此时还有没有遍历到的结点,那么就说明该树非完全二叉树,因为有空洞.C++代码如下: #include "stdafx.

满二叉树 和 完全二叉树

完全二叉树 定义:深度为k,有n个结点的二叉树当且仅当其每一个结点都与深度为k的满二叉树中编号从1至n的结点一一对应时,称为完全二叉树. 特点:叶子结点只可能在层次最大的两层上出现:对任一结点,若其右分支下子孙的最大层次为l,则其左分支下子孙的最大层次必为l 或l+1 满二叉树: 定义:一棵深度为k,且有2的(k)次方-1个节点的二叉树 特点:每一层上的结点数都是最大结点数 区别: 满二叉树 和 完全二叉树,布布扣,bubuko.com

满二叉树的最近公共祖先

满二叉树节点父子之间的关系. //题目描述 // //有一棵无穷大的满二叉树,其结点按根结点一层一层地从左往右依次编号,根结点编号为1.现在有两个结点a,b. //请设计一个算法,求出a和b点的最近公共祖先的编号. //给定两个int a, b.为给定结点的编号.请返回a和b的最近公共祖先的编号.注意这里结点本身也可认为是其祖先. //测试样例: //2,3 //返回:1 //思路:满二叉树的子节点与父节点之间的关系为root = child / 2 //利用这个关系,如果a != b,就让其中

[ACM] sdut 2882 Full Binary Tree (满二叉树的公共祖先)

Full Binary Tree Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 In computer science, a binary tree is a tree data structure in which each node has at most two children. Consider an infinite full binary tree (each node has two children exc

判断任一二叉树,是否为满二叉树.(输出二叉树,节点总数,二叉树深度)

#include "stdio.h"#include "malloc.h"int count;typedef struct node{ char data; struct node *LChild; struct node *RChild;}BiTNode,*BiTree; void creatbitree(BiTree * bt) // 先序便历序列创建二叉树链表{ char ch=getchar(); if (ch=='#') {  *bt=NULL; } el