private static int MaxHeight(Node node) { if (node == null) return 0; int leftHeight = MaxHeight(node.left); int rightHeight = MaxHeight(node.right); if (leftHeight > rightHeight) { return leftHeight + 1; } else { return rightHeight + 1; } }
public static Node ConstructTree() { //Node n1 = new Node(1); //Node n2 = new Node(2); //Node n3 = new Node(3); //Node n4 = new Node(4); //Node n5 = new Node(5); //Node n6 = new Node(6); //Node n7 = new Node(7); Node n1 = new Node(11); Node n2 = new Node(2); Node n3 = new Node(3); Node n4 = new Node(1); Node n5 = new Node(5); Node n6 = new Node(6); Node n7 = new Node(7); Node n8 = new Node(7); Node n9 = new Node(7); Node n10 = new Node(7); Node n11 = new Node(7); Node n12 = new Node(7); Node n13 = new Node(7); n1.left = n2; n1.right = n3; n2.left = n4; n2.right = n5; n3.left = n6; n3.right = n7; n5.right = n8; n8.right = n9; n9.right = n10; n4.right = n11; n11.right = n12; n12.right = n13; return n1; }
private static int GetMaxDiameter(Node node) { int diameter = MaxHeight(node.left) + 1 + MaxHeight(node.right); return diameter; }
public Node(int data) { this.data = data; left = null; right = null; }