-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#76.cc
More file actions
32 lines (31 loc) · 792 Bytes
/
LeetCode#76.cc
File metadata and controls
32 lines (31 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
void dfs(TreeNode* root , int depth , int& res){
if(root == NULL) return ;
if(root->left==NULL && root->right==NULL){
if(res == -1 || depth+1 < res)
res = depth+1;
return ;
}
dfs(root->left,depth+1,res);
dfs(root->right,depth+1,res);
}
public:
int minDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root==NULL) return 0;
int ret = -1;
dfs(root,0,ret);
return ret;
}
};