1、minimum-depth-of-binary-tree

题目描述

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);
}

猜你喜欢

转载自www.cnblogs.com/fuqia/p/9656387.html
今日推荐