author: Blankj blog : http://blankj.com time : 2017/06/05 desc :
| 9 | * </pre> |
| 10 | */ |
| 11 | public class TreeNode { |
| 12 | |
| 13 | public int val; |
| 14 | public TreeNode left; |
| 15 | public TreeNode right; |
| 16 | |
| 17 | public TreeNode(int x) { |
| 18 | val = x; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * 创建测试数据 |
| 23 | * |
| 24 | * @param data [XX,XX,null,xx] |
| 25 | * @return {@link TreeNode} |
| 26 | */ |
| 27 | public static TreeNode createTestData(String data) { |
| 28 | if (data.equals("[]")) return null; |
| 29 | data = data.substring(1, data.length() - 1); |
| 30 | String[] split = data.split(","); |
| 31 | int len = len = split.length; |
| 32 | TreeNode[] treeNodes = new TreeNode[len]; |
| 33 | data = data.substring(1, data.length() - 1); |
| 34 | for (int i = 0; i < len; i++) { |
| 35 | if (!split[i].equals("null")) { |
| 36 | treeNodes[i] = new TreeNode(Integer.valueOf(split[i])); |
| 37 | } |
| 38 | } |
| 39 | for (int i = 0; i < len; i++) { |
| 40 | if (treeNodes[i] != null) { |
| 41 | int leftIndex = i * 2 + 1; |
| 42 | if (leftIndex < len) { |
| 43 | treeNodes[i].left = treeNodes[leftIndex]; |
| 44 | } |
| 45 | int rightIndex = leftIndex + 1; |
| 46 | if (rightIndex < len) { |
| 47 | treeNodes[i].right = treeNodes[rightIndex]; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | return treeNodes[0]; |
| 52 | } |
| 53 | |
| 54 | private static final String space = " "; |
| 55 | |
| 56 | /** |
| 57 | * 竖向打印二叉树 |
| 58 | * |
| 59 | * @param root 二叉树根节点 |
| 60 | */ |
| 61 | public static void print(TreeNode root) { |
| 62 | print(root, 0); |
| 63 | } |
| 64 | |
| 65 | private static void print(TreeNode node, int deep) { |
| 66 | if (node == null) { |
| 67 | printSpace(deep); |
| 68 | System.out.println("#"); |
nothing calls this directly
no outgoing calls
no test coverage detected