时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32M,其他语言64M
求给定二叉树的最小深度。最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。
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.
来自 @Msean
// 运行时间:12ms
// 占用内存:1024k
class Solution {
public:
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);
}
};
Author: Frytea
Title: 编程题minimum-depth-of-binary-tree
Link: https://cloud.tencent.com/developer/article/1662780
Copyright: This work by TL-Song is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.