http://www.1point3acres.com/bbs/thread-141623-1-1.html
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)
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)
public class BinaryTreeWithNLeaf {
static class TreeNode {
public int val;
public TreeNode(final int i) {
val = i;
}
public TreeNode left;
public TreeNode right;
}
public static List<TreeNode> solve(final int n) {
final TreeNode root = new TreeNode(1);
final List<TreeNode> leaves = new ArrayList<TreeNode>();
leaves.add(root);
final HashMap<Integer, List<TreeNode>> record = new HashMap<Integer, List<TreeNode>>();
record.put(1, leaves);
return recurse(n, record);
}
private static List<TreeNode> recurse(final int n, final HashMap<Integer, List<TreeNode>> record) {
if (record.containsKey(n) == true) {
return record.get(n);
}
final 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 (final TreeNode left : leftTrees) {
for (final TreeNode right : rightTrees) {
final 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(final TreeNode root) {
if (root == null) {
return null;
}
final TreeNode node = new TreeNode(root.val); // change name to newRoot
node.left = copyTree(root.left);
node.right = copyTree(root.right);
return node;
}
private static String printLevelTree(final TreeNode root) {
StringBuffer sb = new StringBuffer();
final Queue<TreeNode> qu = new LinkedList<TreeNode>();
qu.add(root);
while (qu.size() != 0) {
final 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(final String[] args) {
final Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
final int n = scan.nextInt();
final List<TreeNode> result = solve(n);
for (final TreeNode node : result) {
System.out.println(printLevelTree(node));
}
}
}
}