1 ms Java Solution - with explanation

  • 0

    I used recursion to solve it. Base case is to check only root node and if it is null then reutn 0

    else call left sub-tree recursively and keep adding 1 for each recursive level
    do same for right subtree

    Finally compare and return the larger value
    public int maxDepth(TreeNode root) {
    if(root==null) {
    return 0;
    int leftDepth = maxDepth(root.left)+1;
    int rightDepth = maxDepth(root.right)+1;

       if(leftDepth>=rightDepth) {
           return leftDepth;
       else {
           return rightDepth;


Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.