Given A binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can SE E ordered from top to bottom.
For example:
Given The following binary tree,
1 <---/ 2 3 <---\ 5 4 <---
You should return [1, 3, 4]
.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode (int x): Val (x), left (null), right (null) {}
* };
*/
Class Solution {
Public
Vector<int> Rightsideview (treenode* root) {
vector<int> Res;
if (root = = nullptr) {
return res;
}
Queue<treenode*> Nodequeue;
Nodequeue.emplace (root);
while (!nodequeue.empty ()) {
int num = Nodequeue.size ();
TreeNode *n;
for (int i = 0; i < num; ++i) {
n = Nodequeue.front ();
Nodequeue.pop ();
if (n->left) {
Nodequeue.emplace (N->left);
}
if (n->right) {
Nodequeue.emplace (N->right);
}
}
Res.emplace_back (N->val);
}
return res;
}
};
[Leetcode] 199-binary Tree right Side View