forked from leetcoders/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinimumDepthofBinaryTree.h
62 lines (57 loc) · 1.75 KB
/
MinimumDepthofBinaryTree.h
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Author: Annie Kim, [email protected]
Date: Apr 6, 2013
Update: Nov 17, 2014 : by King, [email protected]
Problem: Minimum Depth of Binary Tree
Difficulty: easy
Source: http://leetcode.com/onlinejudge#question_111
Notes:
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node
down to the nearest leaf node.
Solution: 1. Recursion. Pay attention to cases when the non-leaf node has only one child.
2. Iteration + Queue. Contributed by SUN Mian(孙冕).
*/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
return minDepth_1(root);
}
int minDepth_1(TreeNode *root) {
if (!root)
return 0;
if (!root->left)
return 1 + minDepth_1(root->right);
if (!root->right)
return 1 + minDepth_1(root->left);
return 1 + min(minDepth_1(root->left), minDepth_1(root->right));
}
int minDepth_2(TreeNode *root) {
if (!root) return 0;
queue<TreeNode *> q;
q.push(root);
TreeNode * rightmost = root;
int depth = 1;
while (!q.empty())
{
TreeNode *node = q.front();
q.pop();
if (!node->left && !node->right) return depth;
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
if (node == rightmost) {
++depth;
rightmost = node->right?node->right:node->left;
}
}
}
};