leetcode-1-Minimum Depth of Binary Tree

来源:互联网 发布:java画流程 编辑:程序博客网 时间:2024/06/03 06:58

知识点:树
题目描述:
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.

思路1:递归解法
class Solution {
public:
int run(TreeNode *root) {
if (root==NULL)
return 0;
int n_left = run(root->left);
int n_right = run(root->right);
if (n_left==0||n_right==0)
return 1+n_left+n_right;
return n_left

原创粉丝点击