美文网首页
[LeetCode] 111. Mininum Depth of

[LeetCode] 111. Mininum Depth of

作者: 弱花 | 来源:发表于2018-11-02 11:19 被阅读0次

    原题

    寻找二叉树最短深度

    思路:
    这里用了dfs,beat 100%,4ms

    class Solution
    {
    public:
      int minDepth(TreeNode *root, int minNum = 0)
      {
        if (root == NULL)
        {
          return minNum == 0 ? minNum : 999999;
        }
        if (root->left == NULL && root->right == NULL)
          return minNum + 1;
        return min(minDepth(root->left, minNum + 1),
                   minDepth(root->right, minNum + 1));
      }
    };
    

    相关文章

      网友评论

          本文标题:[LeetCode] 111. Mininum Depth of

          本文链接:https://www.haomeiwen.com/subject/vzvqxqtx.html