生活会辜负努力的人,但不会辜负一直努力的人

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

题目描述

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
 
int run(TreeNode *root){
    if (root == nullptr) return 0;
    if (root->left == nullptr)
    {
        return run(root->right) + 1;
    }
    if (root->right == nullptr)
    {
        return run(root->left) + 1;
    }
    int leftDepth = run(root->left);
    int rightDepth = run(root->right);
    return (leftDepth <= rightDepth) ? (leftDepth + 1) : (rightDepth + 1);
}

 

posted on 2018-09-16 16:04  何许亻也  阅读(188)  评论(0编辑  收藏  举报