Untitled
unknown
plain_text
3 years ago
890 B
7
Indexable
/* A binary tree node
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};
*/
//Function to return a list containing elements of left view of the binary tree.
vector<int> leftView(Node *root)
{
vector<int> leftView;
if (root == NULL) return leftView;
queue<Node *> q;
q.push(root);
while (!q.empty()) {
int levelSize = q.size();
for (int i = 1; i <= levelSize; i++) {
Node *curr = q.front();
q.pop();
if (i == 1)
leftView.push_back(curr -> data);
if (curr -> left != NULL)
q.push(curr -> left);
if (curr -> right != NULL)
q.push(curr -> right);
}
}
return leftView;
}
Editor is loading...