-
Notifications
You must be signed in to change notification settings - Fork 9
/
MinDepthTree.cpp
42 lines (35 loc) · 1.07 KB
/
MinDepthTree.cpp
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
/*
Min Depth of Binary Tree
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.
NOTE : The path has to end on a leaf node.
Example :
1
/
2
min depth = 2.
*/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
int traverse(TreeNode *root)
{
if (root == NULL) // root is NULL return 0
return 0;
if (!root->left && !root->right) // only root exist return 1
return 1;
if (!root->left) // if left subtree doesnt exist, return 1+size of right subtree
return 1 + traverse(root->right);
if (!root->right) // if right subtree doesnt exist, return 1+size of left subtree
return 1 + traverse(root->left);
return min(traverse(root->left), traverse(root->right)) + 1; // return min of left and right subtree +1
}
int Solution::minDepth(TreeNode *A)
{
return traverse(A);
}