LeetCode 111. Minimum Depth of Binary Tree

来源:互联网 发布:如何破解软件试用期 编辑:程序博客网 时间:2024/05/16 14:18

解题思路:DFS遍历整棵树,取最小深度

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


0 0