Saturday, 2 March 2019

Find Height of Binary Tree in Java.

Find Height of Binary Tree OR Find the Maximum Depth of Binary Tree.


Given a binary tree, Find the height of tree.

Let's understand what is the input and expected output.


Algorithm


In this approach, we will find height of binary tree recursively by following below steps.
  1. For each node of the tree find the height of left subtree and height of right subtree. 
  2. Return the maximum height from left and right subtree +1.

If you want to find height of particular node in Binary tree:   Find Height of node in Binary tree in Java.

Java Program to Find Height of binary tree


FindHeightOfBinaryTree.java
package com.javabypatel.tree;

public class FindHeightOfBinaryTree {
    /*
                Height = 4
                
                     50
                  /      \
                 30       70
               /   \     /  \
              20   40   60  80
             /  \
            10  25
    */
    public static void main(String[] args) {
        new FindHeightOfBinaryTree();
    }

    public FindHeightOfBinaryTree(){
        Node rootNode=null;
        rootNode  = addNode(rootNode, 50);
        rootNode  = addNode(rootNode, 30);
        rootNode  = addNode(rootNode, 70);
        rootNode  = addNode(rootNode, 20);
        rootNode  = addNode(rootNode, 40);
        rootNode  = addNode(rootNode, 10);
        rootNode  = addNode(rootNode, 25);
        rootNode  = addNode(rootNode, 60);
        rootNode  = addNode(rootNode, 80);
        
        int height = findHeightOfTree(rootNode);
        System.out.println(height);
    }

    private int findHeightOfTree(Node rootNode) {
        if(rootNode==null){
            return 0;
        }

        int leftHeight = findHeightOfTree(rootNode.getLeft());
        int rightHeight = findHeightOfTree(rootNode.getRight());

        return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    }

    private Node addNode(Node rootNode, int i) {
        if(rootNode==null){
            return new Node(i);
        }else{
            if(i > rootNode.getData()){
                Node nodeToAdd = addNode(rootNode.getRight(), i);
                rootNode.setRight(nodeToAdd);

            }else{
                Node nodeToAdd = addNode(rootNode.getLeft(), i);
                rootNode.setLeft(nodeToAdd);
            }
        }
        return rootNode;
    }
}
Node.java
package com.javabypatel.tree;

public class Node {
    private int data;
    private Node left;
    private Node right;

    public Node(int data) {
        this.data = data;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }
}

You may also like to see


Top Binary tree interview questions in Java

Diameter of Binary Tree in Java

Find Kth SMALLEST element in BST(Binary Search Tree)

Check whether Binary Tree is foldable or not.

Check if two nodes are cousins in a Binary Tree.

Get Level/Height of node in binary tree

Print nodes at K distance from Leaf node in Binary tree.

Construct a Binary Tree from In-order and Post-order traversals.

Print Nodes in Top View of Binary Tree.

Zig Zag Traversal of Binary Tree.

Find Height of node in Binary tree in Java.

Serialize and Deserialize a Binary Tree


Enjoy !!!! 

If you find any issue in post or face any error while implementing, Please comment.

No comments:

Post a Comment