smilevchy's blog

Life & Study & Chasing

Leetcode_Maximum Depth of Binary Tree

原题链接: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/

public int maxDepth(TreeNode root) {
    if (root == null) return 0;

    if (root.left == null && root.right == null) {
        return 1;
    }

    return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}

algorithm

« Leetcode_Minimum Depth of Binary Tree Leetcode_Balanced Binary Tree »