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] .
Idea: the equivalent of a hierarchical traversal, just note that the last element of the output layer.
void Rightsideview (Bintree *root) {vector<int> VEC; Deque<bintree*> de; if (root = NULL) return; int curnum = 1; int nexnum=0; De.push_back (root); bintree* temp; while (!de.empty ()) {while (Curnum >0) {temp = De.front (); if (Curnum ==1) vec.push_back (Temp->value); De.pop_front (); curnum--; if (temp->left! = NULL) {de.push_back (temp->left); nexnum++; } if (temp->right! = NULL) {de.push_back (temp->right); nexnum++; }} curnum = Nexnum; nexnum=0; } for (int i=0;i<vec.size (); i++) cout<<vec[i]<< ""; cout<<endl; }
Binary Tree Right Side View--leetcode