public static void main(String[] args) { TreeNode t1 = new TreeNode(5); TreeNode t2 = new TreeNode(4); TreeNode t3 = new TreeNode(8); TreeNode t4 = new TreeNode(11); TreeNode t5 = new TreeNode(13); TreeNode t6 = new TreeNode(4); TreeNode t7 = new TreeNode(7); TreeNode t8 = new TreeNode(2); TreeNode t9 = new TreeNode(1); t1.left = t2; t1.right = t3; t2.left = t4; t3.left = t5; t3.right = t6; t4.left = t7; t4.right = t8; t6.right = t9; System.out.println(levelOrderBottom(t1)); }
private TreeNode buildTree(int[] inorder, int[] preorder, int start, int end) { if (end < start) { return null; } TreeNode root = new TreeNode(preorder[preIndex++]); if (start == end) { return root; } int inIndex = search(inorder, start, end, root.val); root.left = buildTree(inorder, preorder, start, inIndex - 1); root.right = buildTree(inorder, preorder, inIndex + 1, end); return root; }
public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(2); root.left.left = new TreeNode(3); root.left.right = new TreeNode(3); root.right.left = new TreeNode(4); root.right.right = new TreeNode(3); List<List<Integer>> res = levelOrder(null); for (List<Integer> r : res) { for (int i : r) { System.out.print(i + "\t"); } System.out.println(); } }
private List<TreeNode> generate(int start, int end) { List<TreeNode> list = new ArrayList<TreeNode>(); if (start <= end) { for (int i = start; i <= end; i++) { List<TreeNode> left = generate(start, i - 1); List<TreeNode> right = generate(i + 1, end); int leftSize = left.size(); int rightSize = right.size(); for (int p = 0; p < leftSize; p++) { for (int q = 0; q < rightSize; q++) { TreeNode node = new TreeNode(i); node.left = left.get(p); node.right = right.get(q); list.add(node); } } } } else list.add(null); return list; }