Why my answer is error

  • 0
        public class Solution {
        public int minDepth(TreeNode root) {
            if(root == null) return 0;
            return Math.min(minDepth(root.left),minDepth(root.right))+1;

  • 1

    Because you program don't consider about this situation, if a tree only have left tree or right tree.

    For example:


    You program will get the wrong answer, since your function will return 0 from right and return 1 from left.
    Then your min will give you 0. So you finally answer will be 1. but the real answer should be 2.

Log in to reply

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