标签:
/** * 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 == NULL) return 0; dep(root,0); } int dep(TreeNode* root,int l){ if(root==NULL) return INT_MAX; if(root -> left ==NULL && root->right ==NULL) return l+1; return min(dep(root->left,l+1),dep(root->right,l+1)); } };
注意题目要求,必须是到叶子结点。
Minimum Depth of Binary Tree 【leetcode】
标签:
原文地址:http://www.cnblogs.com/julie-yang/p/4668725.html