# Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Note: A leaf is a node with no children.

``````var maxDepth = function(root) {
if (root===null) {
return 0;
}
const left=root.left ? maxDepth(root.left) : 0;
const right=root.right ? maxDepth(root.right) : 0;
return 1+Math.max(left,right);
};``````

Approach:
This is a fairly standard Binary Tree recursion problem. We want to find the max depth of the binary tree. This can be broken down to the recursion equation of current node counting as 1 plus the max depth of the left and right children.

Code Explanation
First we set up the terminating condition for the recursion. If the given node is null, we return 0. That’s because a null node isn’t a valid node and has no depth.
Otherwise we take the max of the left and right sub trees, and we add 1 to it in order to count itself.
The left substree is 0 if the left child is null, otherwise we call the maxDepth recursion using that child node as the starting point. Similarly for the right subtree depth.