lintcode二叉树的最小深度

来源:互联网 发布:windows文件夹加密码 编辑:程序博客网 时间:2024/06/03 19:21

二叉树的最小深度 

给定一个二叉树,找出其最小深度。

二叉树的最小深度为根节点到最近叶子节点的距离。
样例

给出一棵如下的二叉树:

        1

     /     \ 

   2       3

          /    \

        4      5  

这个二叉树的最小深度为 2

标签 

相关题目 

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

原创粉丝点击