11.Binary Tree Right Side View
Example:
Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:
1 <---
/ \
2 3 <---
\ \
5 4 <---
Solution : (Level Order traversal last node)
class Solution
{
public:
vector<int> rightSideView(TreeNode *root)
{
vector<int> v;
if(root == NULL)
{
return v;
}
TreeNode *t = NULL;
queue<TreeNode *> q;
q.push(root);
while (!q.empty())
{
int s = q.size();
while (s > 0)
{
t = q.front();
q.pop();
if (t->left)
{
q.push(t->left);
}
if (t->right)
{
q.push(t->right);
}
if (s == 1)
{
v.push_back(t->val);
}
s--;
}
}
return v;
}
};
Last updated