Sunday, March 6, 2022

Size, Sum, Max and Height in a Binary Tree

I/P:

19

50 25 12 n n 37 30 n n n 75 62 n 70 n n 87 n n


O/P:

9

448

87

3








package pep.Day31;

import java.io.*;
import java.util.*;

public class Size_Sum_Max_Height_BinaryTree {
public static class Node {
int data;
Node left;
Node right;

Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}

public static class Pair {
Node node;
int state;

Pair(Node node, int state) {
this.node = node;
this.state = state;
}
}

public static Node construct(Integer[] arr) {
Node root = new Node(arr[0], null, null);
Pair rtp = new Pair(root, 1);

Stack<Pair> st = new Stack<>();
st.push(rtp);

int idx = 0;
while (st.size() > 0) {
Pair top = st.peek();
if (top.state == 1) {
idx++;
if (arr[idx] != null) {
top.node.left = new Node(arr[idx], null, null);
Pair lp = new Pair(top.node.left, 1);
st.push(lp);
} else {
top.node.left = null;
}

top.state++;
} else if (top.state == 2) {
idx++;
if (arr[idx] != null) {
top.node.right = new Node(arr[idx], null, null);
Pair rp = new Pair(top.node.right, 1);
st.push(rp);
} else {
top.node.right = null;
}

top.state++;
} else {
st.pop();
}
}

return root;
}

public static void display(Node node) {
if (node == null) {
return;
}

String str = "";
str += node.left == null ? "." : node.left.data + "";
str += " <- " + node.data + " -> ";
str += node.right == null ? "." : node.right.data + "";
System.out.println(str);

display(node.left);
display(node.right);
}

public static int size(Node node) {
// write your code here
int size = 0;
size += node == null ? 0 : size(node.left) + size(node.right) + 1;
return size;
}

public static int sum(Node node) {
// write your code here
int sum = 0;
sum += node == null ? 0 : sum(node.left) + sum(node.right) + node.data;
return sum;
}

public static int max(Node node) {
// write your code here
if (node == null)
return Integer.MIN_VALUE;
int leftChild = max(node.left);
int rightChild = max(node.right);
int maxChild = Math.max(leftChild, rightChild);
int max = Math.max(node.data, maxChild);
return max;
}

public static int height(Node node) {
// return -1 for edges for base
// return 0 for nodes for base
return node == null ? -1 : Math.max(height(node.left), height(node.right)) + 1;
}

public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
Integer[] arr = new Integer[n];
String[] values = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
if (values[i].equals("n") == false) {
arr[i] = Integer.parseInt(values[i]);
} else {
arr[i] = null;
}
}

Node root = construct(arr);

int size = size(root);
int sum = sum(root);
int max = max(root);
int ht = height(root);
System.out.println(size);
System.out.println(sum);
System.out.println(max);
System.out.println(ht);
}

}




No comments:

Post a Comment

Diagonal Traversal

 eg.  1       2       3       4 5      6       7       8 9    10    11     12 13  14   15    16 Output: 1 6 11 16 2 7 12 3 8 4  Approach:...