# LeetCode-199.Binary Tree Right Side View

https://leetcode.com/problems/binary-tree-right-side-view/

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see 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.
* public class TreeNode {
*     public int val;
*     public TreeNode left;
*     public TreeNode right;
*     public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public IList<int> RightSideView(TreeNode root)
{
IList<int> res = new List<int>();
if (root == null)
return res;
Queue<TreeNode> q = new Queue<TreeNode>();
q.Enqueue(root);
int n;
TreeNode node;
while ((n=q.Count)>0)
{
for (int i = 0; i < n; i++)
{
node = q.Dequeue();
if (node.right != null)
q.Enqueue(node.right);
if (node.left != null)
q.Enqueue(node.left);
}
}
return res;
}
}

C++实现

#include <vector>
#include <queue>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

vector<int> rightSideView(TreeNode* root)
{
vector<int> res;
if (!root)
return res;
queue<TreeNode*> q;
q.push(root);
int n;
TreeNode* node;
while ((n = q.size()) > 0)
{
res.push_back(q.front()->val);
for (int i = 0; i < n; i++)
{
node = q.front();
q.pop();
if (node->right)
q.push(node->right);
if (node->left)
q.push(node->left);
}
}
return res;
}

public class Solution
{
public IList<int> RightSideView(TreeNode root)
{
IList<int> res = new List<int>();
Func(res, root, 0);
return res;
}

private void Func(IList<int> res, TreeNode root, int level)
{
if (root == null)
return;
if (level == res.Count)
Func(res,root.right,level + 1);
Func(res,root.left,level + 1);
}
}

• 本文已收录于以下专栏：

## 【LeetCode】Binary Tree Right Side View 解题报告

【题目】 Given a binary tree, imagine yourself standing on the right side of it, return the values ...
• ljiabin
• 2015年04月06日 10:33
• 8359

## LeetCode 199. Binary Tree Right Side View（二叉树右侧视图）

• jmspan
• 2016年05月04日 03:32
• 668

## Leetcode 199: Binary Tree Right Side View

Problem:Given a binary tree, imagine yourself standing on the right side of it, return the values of...
• sunao2002002
• 2015年04月23日 21:23
• 1035

## Binary Tree Right Side View —— Leetcode（精巧的方法，第二遍）

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod...
• BlitzSkies
• 2015年05月06日 21:55
• 834

## [JAVA]LeetCode199 Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod...
• fumier
• 2015年04月09日 19:41
• 765

## leetcode-199 Binary Tree Right Side View

• sole_cc
• 2015年04月09日 14:32
• 467

## LeetCode199——Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod...
• booirror
• 2015年09月10日 16:09
• 416

## 199. Binary Tree Right Side View Leetcode Python

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod...
• hyperbolechi
• 2015年04月15日 19:11
• 900

## leetcode-java-199. Binary Tree Right Side View

/* 思路： 从右边看，并不是指右节点--也就是每层最右边的节点，则考虑层次遍历只取最右 */ public class Solution { public List rightS...
• github_34514750
• 2016年08月24日 14:51
• 254

## LeetCode---Binary Tree Right Side View

• wzg272365960
• 2015年10月28日 19:41
• 219

举报原因： 您举报文章：LeetCode-199.Binary Tree Right Side View 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)