[Leetcode] Binary Tree Right Side View

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.