|
| 1 | +import java.util.Collection; |
| 2 | +import java.util.Iterator; |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.Queue; |
| 5 | +public class BinaryTreeYT { |
| 6 | + static class Node { |
| 7 | + int data; |
| 8 | + Node left; |
| 9 | + Node right; |
| 10 | + Node(int data) { |
| 11 | + this.data = data; |
| 12 | + this.left = null; |
| 13 | + this.right = null; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + static class BinaryTree { |
| 18 | + static int idx = -1; |
| 19 | + public static Node buildTree(int nodes[]) { |
| 20 | + idx++; |
| 21 | + if (nodes[idx] == -1) { |
| 22 | + return null; |
| 23 | + } |
| 24 | + Node newNode = new Node(nodes[idx]); |
| 25 | + newNode.left = buildTree(nodes); |
| 26 | + newNode.right = buildTree(nodes); |
| 27 | + return newNode; |
| 28 | + } |
| 29 | + } |
| 30 | + public static void preorder(Node root) { |
| 31 | + if (root == null) { |
| 32 | + return; |
| 33 | + } |
| 34 | + System.out.println(root.data + " "); |
| 35 | + preorder(root.left); |
| 36 | + preorder(root.right); |
| 37 | + } |
| 38 | + public static void inorder(Node root) { |
| 39 | + if (root == null) { |
| 40 | + return; |
| 41 | + } |
| 42 | + inorder(root.left); |
| 43 | + System.out.println(root.data + " "); |
| 44 | + inorder(root.right); |
| 45 | + } |
| 46 | + public static void postorder(Node root) { |
| 47 | + if (root == null) { |
| 48 | + return; |
| 49 | + } |
| 50 | + postorder(root.left); |
| 51 | + postorder(root.right); |
| 52 | + System.out.println(root.data + " "); |
| 53 | + ; |
| 54 | + } |
| 55 | + public static void levelOrder(Node root) { |
| 56 | + if (root == null) { |
| 57 | + return; |
| 58 | + } |
| 59 | + Queue<Node> q = new LinkedList<>(); |
| 60 | + q.add(root); |
| 61 | + q.add(null); |
| 62 | + while (!q.isEmpty()) { |
| 63 | + Node currentNode = q.remove(); |
| 64 | + if (currentNode == null) { |
| 65 | + System.out.println(); |
| 66 | + if (q.isEmpty()) { |
| 67 | + break; |
| 68 | + } else { |
| 69 | + q.add(null); |
| 70 | + } |
| 71 | + } else { |
| 72 | + System.out.print(currentNode.data + " "); |
| 73 | + if (currentNode.left != null) { |
| 74 | + q.add(currentNode.left); |
| 75 | + } |
| 76 | + if (currentNode.right != null) { |
| 77 | + q.add(currentNode.right); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + public static int countOfNodes(Node root) { |
| 83 | + if (root == null) { |
| 84 | + return 0; |
| 85 | + } |
| 86 | + int leftNodes = countOfNodes(root.left); |
| 87 | + int rightNodes = countOfNodes(root.right); |
| 88 | + return leftNodes + rightNodes + 1; |
| 89 | + } |
| 90 | + public static int sumOfNodes(Node root) { |
| 91 | + if (root == null) { |
| 92 | + return 0; |
| 93 | + } |
| 94 | + int leftSum = sumOfNodes(root.left); |
| 95 | + int rightSum = sumOfNodes(root.right); |
| 96 | + return leftSum + rightSum + root.data; |
| 97 | + } |
| 98 | + public static int height(Node root) { |
| 99 | + if (root == null) { |
| 100 | + return 0; |
| 101 | + } |
| 102 | + int leftHeight = height(root.left); |
| 103 | + int rightHeight = height(root.right); |
| 104 | + int myHeight = Math.max(leftHeight, rightHeight) + 1; |
| 105 | + return myHeight; |
| 106 | + } |
| 107 | + public static int diameter(Node root) { |
| 108 | + if (root == null) { |
| 109 | + return 0; |
| 110 | + } |
| 111 | + int diam1 = diameter(root.left); |
| 112 | + int diam2 = diameter(root.right); |
| 113 | + int diam3 = height(root.left) + height(root.right) + 1; |
| 114 | + return Math.max(diam3, Math.max(diam1, diam2)); |
| 115 | + } |
| 116 | + static class TreeInfo { |
| 117 | + int ht; |
| 118 | + int diam; |
| 119 | + TreeInfo(int ht, int diam) { |
| 120 | + this.ht = ht; |
| 121 | + this.diam = diam; |
| 122 | + } |
| 123 | + } |
| 124 | + public static TreeInfo diameter2(Node root) { |
| 125 | + if (root == null) { |
| 126 | + return new TreeInfo(0, 0); |
| 127 | + } |
| 128 | + TreeInfo left = diameter2(root.left); |
| 129 | + TreeInfo right = diameter2(root.right); |
| 130 | + int myHeight = Math.max(left.ht, right.ht) + 1; |
| 131 | + int diam1 = left.diam; |
| 132 | + int diam2 = right.diam; |
| 133 | + int diam3 = left.ht + right.ht + 1; |
| 134 | + int mydiam = Math.max(Math.max(diam1, diam2), diam3); |
| 135 | + TreeInfo myInfo = new TreeInfo(myHeight, mydiam); |
| 136 | + return myInfo; |
| 137 | + } |
| 138 | + public boolean isIdentical(Node root, Node subRoot) { |
| 139 | + if (root == null && subRoot == null) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + if (root == null || subRoot == null) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + return isIdentical(root.left, subRoot.left) && isIdentical(root.right, subRoot.right); |
| 146 | + } |
| 147 | + public boolean isSubtree(Node root, Node subRoot) { |
| 148 | + if (subRoot == null) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + if (root == null) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + if (root.data == subRoot.data) { |
| 155 | + if (isIdentical(root, subRoot)) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + } |
| 159 | + if (root.data == subRoot.data) { |
| 160 | + return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot.right); |
| 161 | + } |
| 162 | + return false; |
| 163 | + } |
| 164 | + public static void main(String[] args) { |
| 165 | + int nodes[] = {1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1}; |
| 166 | + BinaryTree tree = new BinaryTree(); |
| 167 | + Node root = tree.buildTree(nodes); |
| 168 | + //System.out.println(root.data); |
| 169 | + //preorder(root); |
| 170 | + //inorder(root); |
| 171 | + //postorder(root); |
| 172 | + //levelOrder(root); |
| 173 | + //System.out.println(countOfNodes(root)); |
| 174 | + //System.out.println(sumOfNodes(root)); |
| 175 | + //System.out.println(height(root)); |
| 176 | + //System.out.println(diameter(root)); |
| 177 | + System.out.println(diameter2(root).diam); |
| 178 | + } |
| 179 | +} |
| 180 | + |
0 commit comments