Tree
Simple Tree Construction
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String[] args) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
}
}
In Order Tree Traversal
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.right.left = new Node(40);
root.right.right = new Node(50);
inorder(root);
}
public static void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.print(root.key + " ");
inorder(root.right);
}
}
}
Pre Order Traversal
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.right.left = new Node(40);
root.right.right = new Node(50);
preorder(root);
}
public static void preorder(Node root) {
if (root != null) {
System.out.print(root.key + " ");
preorder(root.left);
preorder(root.right);
}
}
}
Post Order Traversal
import java.util.*;
import java.io.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.right.left = new Node(40);
root.right.right = new Node(50);
postorder(root);
}
public static void postorder(Node root) {
if (root != null) {
postorder(root.left);
postorder(root.right);
System.out.print(root.key + " ");
}
}
}
Height of Binary Tree
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.right.left = new Node(40);
root.right.right = new Node(50);
System.out.print(height(root));
}
public static int height(Node root) {
if (root == null)
return 0;
else
return (1 + Math.max(height(root.left), height(root.right)));
}
}
Print Nodes at K distance
import java.util.*;
import java.io.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
root.right.right = new Node(70);
root.right.right.right = new Node(80);
int k = 2;
printKDist(root, k);
}
public static void printKDist(Node root, int k) {
if (root == null) return;
if (k == 0) {
System.out.print(root.key + " ");
} else {
printKDist(root.left, k - 1);
printKDist(root.right, k - 1);
}
}
}
Level Order Traversal
import java.util.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
root.right.left = new Node(60);
root.right.right = new Node(70);
printLevel(root);
}
public static void printLevel(Node root) {
if (root == null) return;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
Node curr = q.poll();
System.out.print(curr.key + " ");
if (curr.left != null)
q.add(curr.left);
if (curr.right != null)
q.add(curr.right);
}
}
}
Level Order Traversal Line by Line (Part 1)
import java.util.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
root.right.left = new Node(60);
root.right.right = new Node(70);
printLevel(root);
}
public static void printLevel(Node root) {
if (root == null) return;
Queue<Node> q = new LinkedList<>();
q.add(root);
q.add(null);
while (q.size() > 1) {
Node curr = q.poll();
if (curr == null) {
System.out.println();
q.add(null);
continue;
}
System.out.print(curr.key + " ");
if (curr.left != null)
q.add(curr.left);
if (curr.right != null)
q.add(curr.right);
}
}
}
Level Order Traversal Line by Line (Part 2)
import java.util.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
root.right.left = new Node(60);
root.right.right = new Node(70);
printLevel(root);
}
public static void printLevel(Node root) {
if (root == null) return;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
int count = q.size();
for (int i = 0; i < count; i++) {
Node curr = q.poll();
System.out.print(curr.key + " ");
if (curr.left != null)
q.add(curr.left);
if (curr.right != null)
q.add(curr.right);
}
System.out.println();
}
}
}
Size of Binary Tree
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
root.right.right = new Node(60);
System.out.print(getSize(root));
}
public static int getSize(Node root) {
if (root == null)
return 0;
else
return 1 + getSize(root.left) + getSize(root.right);
}
}
Maximum in Binary Tree
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(20);
root.left = new Node(80);
root.right = new Node(30);
root.right.left = new Node(40);
root.right.right = new Node(50);
System.out.print(getMax(root));
}
public static int getMax(Node root) {
if (root == null)
return Integer.MIN_VALUE;
else
return Math.max(root.key, Math.max(getMax(root.left), getMax(root.right)));
}
}
Print Left View of Tree ( Recursively )
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(20);
root.left = new Node(80);
root.right = new Node(30);
root.right.left = new Node(40);
root.right.right = new Node(50);
System.out.print(getMax(root));
}
public static int getMax(Node root) {
if (root == null)
return Integer.MIN_VALUE;
else
return Math.max(root.key, Math.max(getMax(root.left), getMax(root.right)));
}
}
Print Left View of Tree ( Iteratively )
import java.util.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.right.left = new Node(40);
root.right.right = new Node(50);
printLeft(root);
}
public static void printLeft(Node root) {
if (root == null)
return;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
int count = q.size();
for (int i = 0; i < count; i++) {
Node curr = q.poll();
if (i == 0)
System.out.print(curr.key + " ");
if (curr.left != null)
q.add(curr.left);
if (curr.right != null)
q.add(curr.right);
}
}
}
}
Children Sum Property
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(20);
root.left = new Node(8);
root.right = new Node(12);
root.right.left = new Node(3);
root.right.right = new Node(9);
System.out.print(isCSum(root));
}
public static boolean isCSum(Node root) {
if (root == null)
return true;
if (root.left == null && root.right == null)
return true;
int sum = 0;
if (root.left != null) sum += root.left.key;
if (root.right != null) sum += root.right.key;
return (root.key == sum && isCSum(root.left) && isCSum(root.right));
}
}
Check if tree is balanced
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(30);
root.right.left = new Node(15);
root.right.right = new Node(20);
if (isBalanced(root) > 0)
System.out.print("Balanced");
else
System.out.print("Not Balanced");
}
public static int isBalanced(Node root) {
if (root == null)
return 0;
int lh = isBalanced(root.left);
if (lh == -1) return -1;
int rh = isBalanced(root.right);
if (rh == -1) return -1;
if (Math.abs(lh - rh) > 1)
return -1;
else
return Math.max(lh, rh) + 1;
}
}
Max Width Binary Tree
import java.util.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
root.right.left = new Node(60);
System.out.print(maxWidth(root));
}
public static int maxWidth(Node root) {
if (root == null) return 0;
Queue<Node> q = new LinkedList<>();
q.add(root);
int res = 0;
while (!q.isEmpty()) {
int count = q.size();
res = Math.max(res, count);
for (int i = 0; i < count; i++) {
Node curr = q.poll();
if (curr.left != null)
q.add(curr.left);
if (curr.right != null)
q.add(curr.right);
}
}
return res;
}
}
Convert Binary Tree to Doubly Linked List
import java.util.*;
import java.io.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(20);
root.right.left = new Node(30);
root.right.right = new Node(35);
Node head = BTToDLL(root);
printlist(head);
}
static Node prev = null;
public static Node BTToDLL(Node root) {
if (root == null) return root;
Node head = BTToDLL(root.left);
if (prev == null) {
head = root;
} else {
root.left = prev;
prev.right = root;
}
prev = root;
BTToDLL(root.right);
return head;
}
public static void printlist(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.key + " ");
curr = curr.right;
}
}
}
Construct Binary Tree from Inorder and Preorder
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String[] args) {
int[] in = {20, 10, 40, 30, 50};
int[] pre = {10, 20, 30, 40, 50};
int n = in.length;
Node root = cTree(in, pre, 0, n - 1);
inorder(root);
}
static int preIndex = 0;
public static Node cTree(int in[], int pre[], int is, int ie) {
if (is > ie) return null;
Node root = new Node(pre[preIndex++]);
int inIndex = is;
for (int i = is; i <= ie; i++) {
if (in[i] == root.key) {
inIndex = i;
break;
}
}
root.left = cTree(in, pre, is, inIndex - 1);
root.right = cTree(in, pre, inIndex + 1, ie);
return root;
}
public static void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.print(root.key + " ");
inorder(root.right);
}
}
}
Tree Traversal in Spiral Form (First Method)
import java.util.*;
import java.io.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);
printSpiral(root);
}
public static void printSpiral(Node root) {
if (root == null) return;
Queue<Node> q = new LinkedList<>();
Stack<Integer> s = new Stack<>();
boolean reverse = false;
q.add(root);
while (q.isEmpty() == false) {
int count = q.size();
for (int i = 0; i < count; i++) {
Node curr = q.poll();
if (reverse)
s.add(curr.key);
else
System.out.print(curr.key + " ");
if (curr.left != null)
q.add(curr.left);
if (curr.right != null)
q.add(curr.right);
}
if (reverse) {
while (s.isEmpty() == false) {
System.out.print(s.pop() + " ");
}
}
reverse = !reverse;
}
}
}
Tree Traversal in Spiral Form (Second Method)
import java.util.*;
import java.io.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);
printSpiral(root);
}
public static void printSpiral(Node root) {
if (root == null)
return;
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();
s1.add(root);
while (!s1.isEmpty() || !s2.isEmpty()) {
while (!s1.empty()) {
Node temp = s1.peek();
s1.pop();
System.out.print(temp.key + " ");
if (temp.right != null)
s2.add(temp.right);
if (temp.left != null)
s2.add(temp.left);
}
while (!s2.empty()) {
Node temp = s2.peek();
s2.pop();
System.out.print(temp.key + " ");
if (temp.left != null)
s1.add(temp.left);
if (temp.right != null)
s1.add(temp.right);
}
}
}
}
Diameter of a Binary Tree
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.right.left = new Node(40);
root.right.right = new Node(60);
root.right.left.left = new Node(50);
root.right.right.right = new Node(70);
System.out.println("Height: " + height(root));
System.out.println("Diameter: " + res);
}
static int res = 0;
public static int height(Node root) {
if (root == null)
return 0;
int lh = height(root.left);
int rh = height(root.right);
res = Math.max(res, 1 + lh + rh);
return 1 + Math.max(lh, rh);
}
}
LCA of Binary Tree
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.right.left = new Node(40);
root.right.right = new Node(50);
int n1 = 20, n2 = 50;
Node ans = lca(root, n1, n2);
System.out.println("LCA: " + ans.key);
}
public static Node lca(Node root, int n1, int n2) {
if (root == null) return null;
if (root.key == n1 || root.key == n2)
return root;
Node lca1 = lca(root.left, n1, n2);
Node lca2 = lca(root.right, n1, n2);
if (lca1 != null && lca2 != null)
return root;
if (lca1 != null)
return lca1;
else
return lca2;
}
}
Burn a Binary Tree from a Leaf
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Distance {
int val;
Distance(int d) {
val = d;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
root.right = new Node(30);
root.left.left = new Node(40);
root.left.right = new Node(50);
root.left.left.left = new Node(60);
root.left.left.left.left = new Node(70);
Distance dist = new Distance(-1);
int leaf = 50;
burnTime(root, leaf, dist);
System.out.print(res);
}
static int res = 0;
public static int burnTime(Node root, int leaf, Distance dist) {
if (root == null) return 0;
if (root.key == leaf) {
dist.val = 0;
return 1;
}
Distance ldist = new Distance(-1), rdist = new Distance(-1);
int lh = burnTime(root.left, leaf, ldist);
int rh = burnTime(root.right, leaf, rdist);
if (ldist.val != -1) {
dist.val = ldist.val + 1;
res = Math.max(res, dist.val + rh);
} else if (rdist.val != -1) {
dist.val = rdist.val + 1;
res = Math.max(res, dist.val + lh);
}
return Math.max(lh, rh) + 1;
}
}
Count nodes in a Complete Binary Tree
import java.util.*;
import java.io.*;
import java.lang.*;
class Node
{
int key;
Node left;
Node right;
Node(int k){
key=k;
left=right=null;
}
}
class GFG {
public static void main(String args[])
{
Node root=new Node(10);
root.left=new Node(20);
root.right=new Node(30);
root.right.left=new Node(40);
root.right.right=new Node(50);
System.out.print(countNode(root));
}
public static int countNode(Node root){
int lh=0,rh=0;
Node curr=root;
while(curr!=null){
lh++;
curr=curr.left;
}
curr=root;
while(curr!=null){
rh++;
curr=curr.right;
}
if(lh==rh){
return (int)Math.pow(2,lh)-1;
}else{
return 1+countNode(root.left)+countNode(root.right);
}
}
}
Serialize a Binary Tree
import java.util.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
ArrayList<Integer> arr = new ArrayList<>();
serialize(root, arr);
for (int x : arr) {
System.out.print(x + " ");
}
}
static final int EMPTY = -1;
public static void serialize(Node root, ArrayList<Integer> arr) {
if (root == null) {
arr.add(EMPTY);
return;
}
arr.add(root.key);
serialize(root.left, arr);
serialize(root.right, arr);
}
}
DeSerialize a Binary Tree
import java.util.*;
import java.io.*;
import java.lang.*;
class Node {
int key;
Node left;
Node right;
Node(int k) {
key = k;
left = right = null;
}
}
class Main {
public static void main(String args[]) {
Node root = new Node(10);
root.left = new Node(20);
ArrayList<Integer> arr = new ArrayList<>();
serialize(root, arr);
for (int x : arr) {
System.out.print(x + " ");
}
System.out.println();
;
Node root_new = deSerialize(arr);
inorder(root_new);
}
static final int EMPTY = -1;
public static void serialize(Node root, ArrayList<Integer> arr) {
if (root == null) {
arr.add(EMPTY);
return;
}
arr.add(root.key);
serialize(root.left, arr);
serialize(root.right, arr);
}
static int index = 0;
public static Node deSerialize(ArrayList<Integer> arr) {
if (index == arr.size())
return null;
int val = arr.get(index);
index++;
if (val == EMPTY) return null;
Node root = new Node(val);
root.left = deSerialize(arr);
root.right = deSerialize(arr);
return root;
}
public static void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.print(root.key + " ");
inorder(root.right);
}
}
}
// TODO : iterative pre order traversal and efficient solution to it