class Node{
public char val;
public Node left;
public Node right;
public Node(char val) {
this.val = val;
}
}
public class binaryTree {
public static Node build() {
Node a = new Node('A');
Node b = new Node('B');
Node c = new Node('C');
Node d = new Node('D');
Node e = new Node('E');
Node f = new Node('F');
Node g = new Node('G');
Node h = new Node('H');
a.left = b;
a.right = c;
b.left = d;
b.right = e;
e.left = g;
g.right = h;
c.right = f;
return a;
}
public static void preorder(Node root) {
if (root == null) {
return;
}
System.out.println(root.val + "");
preorder(root.left);
preorder(root.right);
}
public static void inorder(Node root) {
if (root == null) {
return;
}
inorder(root.left);
System.out.println(root.val + "");
inorder(root.right);
}
public static void postorder(Node root) {
if (root == null) {
return;
}
postorder(root.left);
postorder(root.right);
System.out.println(root.val + "");
}
public static int size(Node root) {
if (root == null) {
return 0;
}
return 1 + size(root.left) + size(root.right);
}
public static int leafsize(Node root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
return leafsize(root.left) + leafsize(root.right);
}
public static int KLevelSize(Node root, int k) {
if (root == null || k < 1) {
return 0;
}
if (k == 1) {
return 1;
}
return KLevelSize(root.left, k - 1) + KLevelSize(root.right, k - 1);
}
public static Node find(Node root, char toFind){
if(root == null){
return null;
}
if(root.val == toFind){
return root;
}
Node result = find(root.left,toFind);
if(result != null){
return result;
}
return find(root.right,toFind);
}
}