3. Height of Binary tree

3. Height of Binary tree


Problem link :- click here


package Binary_Tree;

public class BinaryTree__3
{

	static int heightOfTree(Node root)
	{
		if(root == null)
			return 0;
		
		int left_height = heightOfTree(root.left);
		int right_height = heightOfTree(root.right);
		
		return Math.max(left_height, right_height) + 1;
	}
	
	public static void main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		int arr[] = {2, -1, 3, -1, -1, 3};
		tree.root = tree.createTree(arr, 0);
		
		int ans = heightOfTree(tree.root);
		System.out.println("Height : " + ans);
	}

}

Time complexity :- O(n)
Space complexity :- O(n)

Comments

Popular posts from this blog

4.Sort an array of 0's, 1's & 2's.

1. Reverse