111. Minimum Depth of Binary Tree

problem

solution

dfs

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int minDepth(TreeNode* root) {
// dfs
if(!root) return 0;
if(!root->left) return 1+minDepth(root->right);
if(!root->right) return 1+minDepth(root->left);
return 1+min(minDepth(root->left), minDepth(root->right));
}
};

bfs

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
class Solution {
public:
int minDepth(TreeNode* root) {
// bfs
if(!root) return 0;
queue<TreeNode*> q;
q.push(root);
int minDepth = 1;
while(!q.empty()){
int size = q.size();
for(int i=0;i<size; ++i){
TreeNode *p = q.front();
q.pop();

if(!p->left && !p->right) return minDepth;
else if(p->left && p->right){
q.push(p->left);
q.push(p->right);
}
else if(!p->left){
q.push(p->right);
}
else if(!p->right){
q.push(p->left);
}

}
minDepth++;
}
return minDepth;
}
};

analysis

  • backtracking
    time complexity O(n)
    space complexity O(logN)
  • bfs
    time complexity O(n)
    space complexity O(n) , N/2 樹最底層的節點數量