1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| package com.colin.tool;
import java.util.List; import java.util.Queue; import java.util.concurrent.LinkedBlockingQueue;
public class LeetCode {
public static class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; } }
public static class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int x) { val = x; } }
public static ListNode createList(String str){ if (str==null) return null; String substring = str.substring(1, str.length() - 1); if (substring.length()==0)return null; String[] split = substring.split(","); ListNode root = new ListNode(-1); ListNode temp = root; for (String s : split) { ListNode next = new ListNode(Integer.parseInt(s)); temp.next = next; temp = next; } return root.next; }
public static TreeNode createTree(String str) { if (str==null) return null; String substring = str.substring(1, str.length() - 1); if (substring.length()==0)return null; String[] split = substring.split(","); TreeNode root = split[0].equals("null")?null:new TreeNode(Integer.parseInt(split[0])); Queue<TreeNode> queue = new LinkedBlockingQueue<>(); queue.add(root); int index = 1; while (!queue.isEmpty()){ TreeNode cur = queue.poll(); if (index<split.length){ cur.left = split[index].equals("null")?null:new TreeNode(Integer.parseInt(split[index])); index++; if (cur.left!=null){ queue.add(cur.left); } }else {break;} if (index<split.length){ cur.right= split[index].equals("null")?null:new TreeNode(Integer.parseInt(split[index])); index++; if (cur.right!=null){ queue.add(cur.right); } }else {break;} } return root; } public static void show(List<TreeNode> nodes) { nodes.forEach(node-> System.out.print(node.val + ",")); System.out.println(); }
public static void show(TreeNode root) { if (root == null) System.out.println("EMPTY!"); int treeDepth = getTreeDepth(root);
int arrayHeight = treeDepth * 2 - 1; int arrayWidth = (2 << (treeDepth - 2)) * 3 + 1; String[][] res = new String[arrayHeight][arrayWidth]; for (int i = 0; i < arrayHeight; i ++) { for (int j = 0; j < arrayWidth; j ++) { res[i][j] = " "; } }
writeArray(root, 0, arrayWidth/ 2, res, treeDepth);
for (String[] line: res) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < line.length; i ++) { sb.append(line[i]); if (line[i].length() > 1 && i <= line.length - 1) { i += line[i].length() > 4 ? 2: line[i].length() - 1; } } System.out.println(sb.toString()); } }
public static int getTreeDepth(TreeNode root) { return root == null ? 0 : (1 + Math.max(getTreeDepth(root.left), getTreeDepth(root.right))); }
private static void writeArray(TreeNode currNode, int rowIndex, int columnIndex, String[][] res, int treeDepth) { if (currNode == null) return; res[rowIndex][columnIndex] = String.valueOf(currNode.val);
int currLevel = ((rowIndex + 1) / 2); if (currLevel == treeDepth) return; int gap = treeDepth - currLevel - 1;
if (currNode.left != null) { res[rowIndex + 1][columnIndex - gap] = "/"; writeArray(currNode.left, rowIndex + 2, columnIndex - gap * 2, res, treeDepth); }
if (currNode.right != null) { res[rowIndex + 1][columnIndex + gap] = "\\"; writeArray(currNode.right, rowIndex + 2, columnIndex + gap * 2, res, treeDepth); } } }
|