|
| 1 | +// package supports; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.Queue; |
| 5 | + |
| 6 | +public class TreeNode { |
| 7 | + public int val; |
| 8 | + public TreeNode left; |
| 9 | + public TreeNode right; |
| 10 | + public TreeNode(int x) { val = x; } |
| 11 | + |
| 12 | + public static TreeNode constructTreeFromString(String str) { |
| 13 | + String[] strs = str.split(","); |
| 14 | + int L = strs.length; |
| 15 | + if(L < 1) return null; |
| 16 | + |
| 17 | + TreeNode root = new TreeNode(Integer.parseInt(strs[0])); |
| 18 | + |
| 19 | + Queue<TreeNode> q = new LinkedList<>(); |
| 20 | + q.add(root); |
| 21 | + int i = 0; |
| 22 | + while(i<L-1) { |
| 23 | + TreeNode current = q.peek(); |
| 24 | + q.poll(); |
| 25 | + if(i<L-1 && !strs[++i].equals("#")) { |
| 26 | + current.left = new TreeNode(Integer.parseInt(strs[i])); |
| 27 | + current.left.left = null; |
| 28 | + current.left.right = null; |
| 29 | + q.add(current.left); |
| 30 | + } |
| 31 | + if(i<L-1 && !strs[++i].equals("#")) { |
| 32 | + current.right = new TreeNode(Integer.parseInt(strs[i])); |
| 33 | + current.right.left = null; |
| 34 | + current.right.right = null; |
| 35 | + q.add(current.right); |
| 36 | + } |
| 37 | + } |
| 38 | + return root; |
| 39 | + } |
| 40 | + |
| 41 | + public static void preorderTraversal(TreeNode root) { |
| 42 | + //TreeNode root = this; |
| 43 | + //if(root == null) return; |
| 44 | + System.out.print(root.val + "\t"); |
| 45 | + if(root.left!=null) |
| 46 | + preorderTraversal(root.left); |
| 47 | + if(root.right!=null) |
| 48 | + preorderTraversal(root.right); |
| 49 | + } |
| 50 | + public static void inorderTraversal(TreeNode root) { |
| 51 | + //TreeNode root = this; |
| 52 | + if(root == null) return; |
| 53 | + if(root.left!=null) |
| 54 | + inorderTraversal(root.left); |
| 55 | + System.out.print(root.val + "\t"); |
| 56 | + if(root.right!=null) |
| 57 | + inorderTraversal(root.right); |
| 58 | + } |
| 59 | + public static void postorderTraversal(TreeNode root) { |
| 60 | + //TreeNode root = this; |
| 61 | + if(root == null) return; |
| 62 | + if(root.left!=null) |
| 63 | + postorderTraversal(root.left); |
| 64 | + if(root.right!=null) |
| 65 | + postorderTraversal(root.right); |
| 66 | + System.out.print(root.val + "\t"); |
| 67 | + } |
| 68 | + |
| 69 | + public static String toString(TreeNode root) { |
| 70 | + return toString(root, new StringBuffer(), true, new StringBuffer()).toString(); |
| 71 | + } |
| 72 | + public static StringBuffer toString(TreeNode root, StringBuffer prefix, boolean isTail, StringBuffer sb) { |
| 73 | + if(root.right!=null) toString(root.right, new StringBuffer().append(prefix).append(isTail ? "©¦ " : " "), false, sb); |
| 74 | + String str = " " + root.val + " "; |
| 75 | + sb.append(prefix).append(isTail ? "©¸©¤©¤" : "©°©¤©¤").append(str).append("\n"); |
| 76 | + if(root.left!=null) toString(root.left, new StringBuffer().append(prefix).append(isTail ? " " : "©¦ "), true, sb); |
| 77 | + return sb; |
| 78 | + } |
| 79 | +} |
0 commit comments