forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumDepthOfABinaryTree.cpp
More file actions
89 lines (76 loc) · 1.88 KB
/
MinimumDepthOfABinaryTree.cpp
File metadata and controls
89 lines (76 loc) · 1.88 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// C++ program to find minimum depth of a given Binary Tree
#include<bits/stdc++.h>
using namespace std;
// A Binary Tree Node
struct Node
{
int data;
struct Node *left, *right;
};
// A queue item (Stores pointer to node and an integer)
struct qItem
{
Node *node;
int depth;
};
// Iterative method to find minimum depth of Binary Tree
int minDepth(Node *root)
{
// Corner Case
if (root == NULL)
return 0;
// Create an empty queue for level order traversal
queue<qItem> q;
// Enqueue Root and initialize depth as 1
qItem qi = {root, 1};
q.push(qi);
// Do level order traversal
while (q.empty() == false)
{
// Remove the front queue item
qi = q.front();
q.pop();
// Get details of the remove item
Node *node = qi.node;
int depth = qi.depth;
// If this is the first leaf node seen so far
// Then return its depth as answer
if (node->left == NULL && node->right == NULL)
return depth;
// If left subtree is not NULL, add it to queue
if (node->left != NULL)
{
qi.node = node->left;
qi.depth = depth + 1;
q.push(qi);
}
// If right subtree is not NULL, add it to queue
if (node->right != NULL)
{
qi.node = node->right;
qi.depth = depth+1;
q.push(qi);
}
}
return 0;
}
// Utility function to create a new tree Node
Node* newNode(int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Driver program to test above functions
int main()
{
// Let us create binary tree shown in above diagram
Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
cout << minDepth(root);
return 0;
}