Long time no write, recently busy graduation thesis.
This question, that is, a binary tree, you see from the right, you can see what the number (will be obscured)
In fact, abstract it means ... Binary tree What is the rightmost number of each layer?
Well, let's go through the layers once.
/** Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * T Reenode (int x): Val (x), left (null), right (NULL) {} *}; */classSolution { Public: Vector<int> Rightsideview (TreeNode *root) {Vector<int>ans; if(Root = = nullptr)returnans; intCNT =1; Queue<TreeNode*>que; Que.push (root); TreeNode*Curr; while(!Que.empty ()) { intnow_cnt =0; for(inti =0; I < CNT; i++) {Curr=Que.front (); Que.pop (); if(curr->Left ) {Que.push (Curr-Left ); Now_cnt++; } if(curr->Right ) {Que.push (Curr-Right ); Now_cnt++; }} ans.push_back (Curr-val); CNT=now_cnt; } returnans; }};
[Leetcode] Binary Tree Right Side View