Generate Trees With N Leaves
given number N which represents total number of leaves in tree. you need to generate all possible tree,
such that each node in tree has zero child or two children. The return type should be a list of such kind of trees.
Only tree structure matters, tree node doesn't have any value initially.
My solution: N = 1 and N = 2 are base cases
For example, N = 3.
1 1
/ \ / \
1 1 1 1
/ \ / \
1 1 1 1
For N = 4, all possible trees can be generated from f(3) by attaching each leaf node with two children,
recursively follow this pattern to return target N. (Note: f(3) indicates a list of trees with 3 nodes in structure described above)
使用了递归+打表的方法
import java.util.*;
public class BinaryTreeWithNLeaf {
public static List<TreeNode> solve(int n) {
TreeNode root = new TreeNode(1);
List<TreeNode> leaves = new ArrayList<TreeNode>();
leaves.add(root);. 1point3acres.com/bbs
HashMap<Integer, List<TreeNode>> record = new HashMap<Integer, List<TreeNode>>();
record.put(1, leaves);
return recurse(n, record);
}
private static List<TreeNode> recurse(int n, HashMap<Integer, List<TreeNode>> record) {
if (record.containsKey(n) == true)
return record.get(n);
List<TreeNode> result = new ArrayList<TreeNode>();
for (int i = 1; i <= n-1; ++i) {
List<TreeNode> leftTrees, rightTrees;
if (record.containsKey(i) == true) {
leftTrees = record.get(i);
} else {
leftTrees = recurse(i, record);
}
if (record.containsKey(n-i) == true) {
rightTrees = record.get(n-i);
} else {
rightTrees = recurse(n-i, record);
}
for (TreeNode left : leftTrees) {
for (TreeNode right : rightTrees) {
TreeNode root = new TreeNode(1);
root.left = copyTree(left);
root.right = copyTree(right);
result.add(root);
}
}
}
record.put(n, result);
return result;
}
private static TreeNode copyTree(TreeNode root) {
if (root == null)
return null;
TreeNode node = new TreeNode(root.val);
node.left = copyTree(root.left);
node.right = copyTree(root.right);
return node;. more info on 1point3acres.com
}
private static String printLevelTree(TreeNode root) {
StringBuffer sb = new StringBuffer();
Queue<TreeNode> qu = new LinkedList<TreeNode>();
qu.add(root);
while (qu.size() != 0) {
TreeNode node = qu.poll();
if (node == null)
sb.append("#,");
else {
sb.append(node.val + ",");
qu.add(node.left);
qu.add(node.right);
}
}
sb = sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int n = scan.nextInt();
List<TreeNode> result = solve(n);
for (TreeNode node : result) {
System.out.println(printLevelTree(node));
}
}
}
}
given number N which represents total number of leaves in tree. you need to generate all possible tree,
such that each node in tree has zero child or two children. The return type should be a list of such kind of trees.
Only tree structure matters, tree node doesn't have any value initially.
My solution: N = 1 and N = 2 are base cases
For example, N = 3.
1 1
/ \ / \
1 1 1 1
/ \ / \
1 1 1 1
For N = 4, all possible trees can be generated from f(3) by attaching each leaf node with two children,
recursively follow this pattern to return target N. (Note: f(3) indicates a list of trees with 3 nodes in structure described above)
使用了递归+打表的方法
import java.util.*;
public class BinaryTreeWithNLeaf {
public static List<TreeNode> solve(int n) {
TreeNode root = new TreeNode(1);
List<TreeNode> leaves = new ArrayList<TreeNode>();
leaves.add(root);. 1point3acres.com/bbs
HashMap<Integer, List<TreeNode>> record = new HashMap<Integer, List<TreeNode>>();
record.put(1, leaves);
return recurse(n, record);
}
private static List<TreeNode> recurse(int n, HashMap<Integer, List<TreeNode>> record) {
if (record.containsKey(n) == true)
return record.get(n);
List<TreeNode> result = new ArrayList<TreeNode>();
for (int i = 1; i <= n-1; ++i) {
List<TreeNode> leftTrees, rightTrees;
if (record.containsKey(i) == true) {
leftTrees = record.get(i);
} else {
leftTrees = recurse(i, record);
}
if (record.containsKey(n-i) == true) {
rightTrees = record.get(n-i);
} else {
rightTrees = recurse(n-i, record);
}
for (TreeNode left : leftTrees) {
for (TreeNode right : rightTrees) {
TreeNode root = new TreeNode(1);
root.left = copyTree(left);
root.right = copyTree(right);
result.add(root);
}
}
}
record.put(n, result);
return result;
}
private static TreeNode copyTree(TreeNode root) {
if (root == null)
return null;
TreeNode node = new TreeNode(root.val);
node.left = copyTree(root.left);
node.right = copyTree(root.right);
return node;. more info on 1point3acres.com
}
private static String printLevelTree(TreeNode root) {
StringBuffer sb = new StringBuffer();
Queue<TreeNode> qu = new LinkedList<TreeNode>();
qu.add(root);
while (qu.size() != 0) {
TreeNode node = qu.poll();
if (node == null)
sb.append("#,");
else {
sb.append(node.val + ",");
qu.add(node.left);
qu.add(node.right);
}
}
sb = sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int n = scan.nextInt();
List<TreeNode> result = solve(n);
for (TreeNode node : result) {
System.out.println(printLevelTree(node));
}
}
}
}