104. Maximum Depth of Binary Tree
  • If we have max depth of left and max depth of right then by comparing them we got max depth of subtree in which adding 1 to get final ans
  • recursive approach is to calculate left subtree max depth , and right subtree max depth after that find max from that and add 1 into it for root node
class Solution {
    public int maxDepth(TreeNode root) {
	// base case
      if(root == null)
          return 0;
       return Math.max( maxDepth( root.left ) , maxDepth( root.right ) ) + 1; 
    }
 }
Comments (0)