need cut branch 111. Minimum Depth of Binary Tree

来源:互联网 发布:学java好还是c 好 编辑:程序博客网 时间:2024/05/20 23:37
/**  * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    int minDepth(TreeNode* root) {        if(!root) return 0;        if(!root->left&&!root->right) return 1;        int l=0;        if(root->left)            l=minDepth(root->left);        int r=0;        if(root->right)            r=minDepth(root->right);        if(!l) return r+1;        if(!r) return l+1;        if(l<r) return l+1;        return r+1;    }};
0 0
原创粉丝点击