Binary tree
class BTNode {
public char val;
public BTNode left;
public BTNode right;
public BTNode(char val) {
this.val = val;
}
}
public class BinaryTree {
public BTNode createTree() {
BTNode A = new BTNode('A');
BTNode B = new BTNode('B');
BTNode C = new BTNode('C');
BTNode D = new BTNode('D');
BTNode E = new BTNode('E');
BTNode F = new BTNode('F');
BTNode G = new BTNode('G');
BTNode H = new BTNode('H');
A.left = B;
A.right = C;
B.left = D;
B.right = E;
E.right = H;
C.left = F;
C.right = G;
return A;
}
void preOrderTraversal(BTNode root){
if(root == null) return;
System.out.print(root.val);
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
void inOrderTraversal(BTNode root){
if(root == null) return;
inOrderTraversal(root.left);
System.out.print(root.val);
inOrderTraversal(root.right);
}
void postOrderTraversal(BTNode root){
if(root == null) return;
postOrderTraversal(root.left);
postOrderTraversal(root.right);
System.out.print(root.val);
}
static int size = 0;
void getSize1(BTNode root) {
if(root == null) return;
size++;
getSize1(root.left);
getSize1(root.right);
}
int getSize2(BTNode root) {
if(root == null) return 0;
return getSize2(root.left) + getSize2(root.right) + 1;
}
static int leafSize = 0;
void getLeafSize1(BTNode root) {
if(root == null) return;
if(root.left == null && root.right == null) {
leafSize++;
}
getLeafSize1(root.left);
getLeafSize1(root.right);
}
int getLeafSize2(BTNode root) {
if(root == null) return 0;
if(root.left == null && root.right == null) {
return 1;
}
return getLeafSize2(root.left) + getLeafSize2(root.right);
}
int getKLevelSize(BTNode root, int k) {
if(root == null) return 0;
if(k == 1) {
return 1;
}
return getKLevelSize(root.left,k-1) + getKLevelSize(root.right,k-1);
}
int getHeight(BTNode root) {
if(root == null) return 0;
if(root.left == null && root.right == null) {
return 1;
}
int lefthigh = getHeight(root.left);
int righthigh = getHeight(root.right);
int max = Math.max(lefthigh,righthigh);
return max+1;
}
int getHeight2(BTNode root) {
if(root == null) return 0;
int lefthight = getHeight(root.left);
int righthight = getHeight(root.right);
return lefthight > righthight ? lefthight+1 : righthight+1;
}
BTNode find(BTNode root, char val) {
if (root == null) return null;
if(root.val == val) return root;
BTNode a = find(root.left, val);
if(a != null) {
return a;
}
BTNode b = find(root.right, val);
if(b != null) {
return b;
}
return null;
}
void levelOrderTraversal(BTNode root) {
if(root == null) return;
Queue<BTNode> queue = new LinkedList<>();
queue.offer(root);
BTNode cur = root;
while(!queue.isEmpty()) {
cur = queue.poll();
System.out.println(cur.val);
if(cur.left != null) {
queue.offer(cur.left);
}
if(cur.right != null) {
queue.offer(cur.right);
}
}
}
boolean isCompleteTree(BTNode root) {
if (root == null) return false;
Queue<BTNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
BTNode cur = queue.poll();
if(cur == null) {
break;
}else {
queue.offer(cur.left);
queue.offer(cur.right);
}
}
while(!queue.isEmpty()) {
BTNode cur = queue.poll();
if(cur != null) return false;
}
return true;
}
void preOrderTraversalNor(BTNode root) {
if(root == null) return;
Stack<BTNode> stack = new Stack<>();
BTNode cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
System.out.println(cur.val + " ");
cur = cur.left;
}
BTNode top = stack.pop();
cur = top.right;
}
System.out.println();
}
void inOrderTraversalNor(BTNode root) {
if(root == null) return;
Stack<BTNode> stack = new Stack<>();
BTNode cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
BTNode top = stack.pop();
System.out.println(top.val + " ");
cur = top.right;
}
System.out.println();
}
void postOrderTraversalNor(BTNode root) {
if(root == null) return;
Stack<BTNode> stack = new Stack<>();
BTNode cur = root;
BTNode prev = null;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
BTNode top = stack.peek();
if(top.right == null || top.right == prev) {
stack.pop();
System.out.println(top.val + " ");
prev = top;
}else {
cur = top.right;
}
}
System.out.println();
}
}