My accepted Solution


  • 0
    K
    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int maxDepth(TreeNode root) {
                return recursion(root, 0);
            
        }
            public int recursion(TreeNode root, int depth){
                if(root==null) return depth;
                else{
                    depth++;
                    return Math.max(recursion(root.right,depth), recursion(root.left,depth));
                   }
            }
    }

  • 0
    L
    int maxDepth(TreeNode *root) {
            if(!root)
             return 0;
             
            return 1+max(maxDepth(root->right),maxDepth(root->left));
        }
    

    There is no need of taking extra parameters in the func signature.


  • 0
    S

    Thanks for your post. However it would be better to share solution with correct code format and elaborated thoughts. Please read the Discuss FAQ for more info. Take a look at good sharing example


  • 0
    W

    You call the library function 'max', and it did same work as you implemented comparison in function signature, call function would pay the price, your code is more concise, but it would not be the best.


Log in to reply
 

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