Max Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth of a binary tree is the number of nodes along the longest path from the root node down to the farthest leaf node.

NOTE : The path has to end on a leaf node. 
Example :

         1
        /
       2

max depth = 2.

Method:

DFS

Solution:

Time: O(n)
Space: O(1)

/**
 * Definition for binary tree
 * class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) {
 *      val = x;
 *      left=null;
 *      right=null;
 *     }
 * }
 */
public class Solution {
    public int maxDepth(TreeNode A) {
        if (A == null) {
            return 0;
        }
        if (A.left == null && A.right == null) {
            return 1;
        }
        if (A.left == null) {
            return maxDepth(A.right) + 1;
        }
        if (A.right == null) {
            return maxDepth(A.left) + 1;
        }
        int left = maxDepth(A.left);
        int right = maxDepth(A.right);
        return Math.max(left, right) + 1;
    }
}