Embed presentation
Download to read offline
![Write a program that finds the max binary tree height.
Solution
/**
* @author Srinivas Palli
*
*/
public class CaliculatTreeHeight {
/**
* to get height of the tree
*
* @param root
* @return
*/
public int treeHeight(Node root) {
if (root == null)
return 0;
return (1 + Math.max(treeHeight(root.left), treeHeight(root.right)));
}
/**
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws java.lang.Exception {
Node root = new Node(5);
root.left = new Node(10);
root.right = new Node(15);
root.left.left = new Node(20);
root.left.right = new Node(25);
root.left.left.left = new Node(30);
root.left.right.left = new Node(35);
root.left.right.left.left = new Node(40);
root.left.right.left.left.right = new Node(45);
root.left.right.left.left.right.left = new Node(50);](https://image.slidesharecdn.com/writeaprogramthatfindsthemaxbinarytreeheight-solution-a-230205071123-f1ddfc7b/75/Write-a-program-that-finds-the-max-binary-tree-height-Solution-a-docx-1-2048.jpg)

Write a program that finds the max binary tree height. Solution /** * @author Srinivas Palli * */ public class CaliculatTreeHeight { /** * to get height of the tree * * @param root * @return */ public int treeHeight(Node root) { if (root == null) return 0; return (1 + Math.max(treeHeight(root.left), treeHeight(root.right))); } /** * @param args * @throws java.lang.Exception */ public static void main(String[] args) throws java.lang.Exception { Node root = new Node(5); root.left = new Node(10); root.right = new Node(15); root.left.left = new Node(20); root.left.right = new Node(25); root.left.left.left = new Node(30); root.left.right.left = new Node(35); root.left.right.left.left = new Node(40); root.left.right.left.left.right = new Node(45); root.left.right.left.left.right.left = new Node(50); CaliculatTreeHeight caliculatTreeHeight = new CaliculatTreeHeight(); System.out.println(\"Height of the Tree is :\" + caliculatTreeHeight.treeHeight(root)); } } /** * @author Srinivas Palli * */ class Node { int data; Node left; Node right; /** * @param data */ public Node(int data) { this.data = data; this.left = null; this.right = null; } } OUTPUT: Height of the Tree is :7 .
![Write a program that finds the max binary tree height.
Solution
/**
* @author Srinivas Palli
*
*/
public class CaliculatTreeHeight {
/**
* to get height of the tree
*
* @param root
* @return
*/
public int treeHeight(Node root) {
if (root == null)
return 0;
return (1 + Math.max(treeHeight(root.left), treeHeight(root.right)));
}
/**
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws java.lang.Exception {
Node root = new Node(5);
root.left = new Node(10);
root.right = new Node(15);
root.left.left = new Node(20);
root.left.right = new Node(25);
root.left.left.left = new Node(30);
root.left.right.left = new Node(35);
root.left.right.left.left = new Node(40);
root.left.right.left.left.right = new Node(45);
root.left.right.left.left.right.left = new Node(50);](https://image.slidesharecdn.com/writeaprogramthatfindsthemaxbinarytreeheight-solution-a-230205071123-f1ddfc7b/75/Write-a-program-that-finds-the-max-binary-tree-height-Solution-a-docx-1-2048.jpg)
