具有 n 个节点的所有可能的完整二叉树的列表
A list of all possible full binary trees with n nodes
这是link问题:All Possible Full Binary Trees。
Given an integer n
, return a list of all possible full binary trees with n
nodes. Each node of each tree in the answer must have Node.val == 0
.
Each element of the answer is the root node of one possible tree. You may return the final list of trees in any order.
A full binary tree is a binary tree where each node has exactly 0
or 2
children.
Example 1:
Input: n = 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]]
在这个问题中,我必须 return 所有可能的完整二叉树的列表,这是我的 java 解决方案代码,任何人都可以帮助我找出我的代码出错的地方,对于每个输入,我的代码都给出空列表。
class Solution {
public List<TreeNode> allPossibleFBT(int n) {
if (n == 1) {
List<TreeNode> list = new ArrayList<TreeNode>();
list.add(new TreeNode(0));
return list;
}
List<TreeNode> left;
List<TreeNode> right;
List<TreeNode> all = new ArrayList<TreeNode>();
for (int i = 1; i < n; i = i + 2) {
left = allPossibleFBT(i);
right = allPossibleFBT(n - i - 1);
for (int k = 0; i < left.size(); k++) {
for (TreeNode treeNode : right) {
TreeNode root = new TreeNode(0);
root.left = left.get(k);
root.right = treeNode;
all.add(root);
}
}
}
return all;
}
}
树节点Class:
/**
* Definition for a binary tree node.
*/
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() { }
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
在 for(int k=0;i<left.size();k++)
中,您需要将 i
替换为 k
。
这是link问题:All Possible Full Binary Trees。
Given an integer
n
, return a list of all possible full binary trees withn
nodes. Each node of each tree in the answer must haveNode.val == 0
.Each element of the answer is the root node of one possible tree. You may return the final list of trees in any order.
A full binary tree is a binary tree where each node has exactly
0
or2
children.Example 1:
Input:
n = 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]]
在这个问题中,我必须 return 所有可能的完整二叉树的列表,这是我的 java 解决方案代码,任何人都可以帮助我找出我的代码出错的地方,对于每个输入,我的代码都给出空列表。
class Solution {
public List<TreeNode> allPossibleFBT(int n) {
if (n == 1) {
List<TreeNode> list = new ArrayList<TreeNode>();
list.add(new TreeNode(0));
return list;
}
List<TreeNode> left;
List<TreeNode> right;
List<TreeNode> all = new ArrayList<TreeNode>();
for (int i = 1; i < n; i = i + 2) {
left = allPossibleFBT(i);
right = allPossibleFBT(n - i - 1);
for (int k = 0; i < left.size(); k++) {
for (TreeNode treeNode : right) {
TreeNode root = new TreeNode(0);
root.left = left.get(k);
root.right = treeNode;
all.add(root);
}
}
}
return all;
}
}
树节点Class:
/**
* Definition for a binary tree node.
*/
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() { }
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
在 for(int k=0;i<left.size();k++)
中,您需要将 i
替换为 k
。