Table of contents
Left View Of Binary Tree
Problem Link:
Hint video:
Code:
vector<int> printLeftView(BinaryTreeNode<int>* root) {
// Write your code here.
vector<int> view;
queue<pair<BinaryTreeNode<int>*,int>> q;
if(root!=NULL){
q.push({root,0});
}
while(!q.empty()){
BinaryTreeNode<int>* node = q.front().first;
int height = q.front().second;
q.pop();
if(height+1>view.size()){
view.push_back(node->data);
}
if(node->left!=NULL){
q.push({node->left,height+1});
}
if(node->right!=NULL){
q.push({node->right,height+1});
}
}
return view;
}