[Leetcode]Binary Tree Right Side View(TAT)

原创 2015年11月19日 18:59:47

Binary Tree Right Side View My Submissions Question
Total Accepted: 26870 Total Submissions: 87779 Difficulty: Medium
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].

Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.

Subscribe to see which companies asked this question
这道题终于一次AC了!!!!!!在被各种深搜贪心二分查找虐了一脸翔之后终于扬眉吐气了一把!!!

/**
 * 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;
        //vector<int> tem;
        if(!root)   return res;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()){
            for(int i = 0,len = q.size();i < len;++i){
                TreeNode* t = q.front();
                q.pop();
                if(i == len - 1){
                    res.push_back(t->val);
                }
                if(t->left) q.push(t->left);
                if(t->right)    q.push(t->right);
            }
            //res.push_back(*(tem.end() - 1));
            //tem.clear();
        }
        return res;
    }
};

其实就是层序遍历,把每层的最后一个数字放进结果里,可以省略一下每层的tem

版权声明:转载请注明出处

LeetCode 199. Binary Tree Right Side View(二叉树右侧视图)

原题网址:https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yo...
  • jmspan
  • jmspan
  • 2016年05月04日 03:32
  • 662

[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
  • fumier
  • 2015年04月09日 19:41
  • 761

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
  • sunao2002002
  • 2015年04月23日 21:23
  • 1035

leetcode-199 Binary Tree Right Side View

这题的意思就是返回每层的最右节点的值,所以使用层序遍历。这就引出了一个问题,如何标志每一层,这里使用了两个变量current和next表示当前层的节点的个数和下一层的节点的个数,也可以使用两个队列(参...
  • sole_cc
  • sole_cc
  • 2015年04月09日 14:32
  • 467

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

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

leetcode-java-199. Binary Tree Right Side View

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

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
  • BlitzSkies
  • 2015年05月06日 21:55
  • 833

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
  • booirror
  • 2015年09月10日 16:09
  • 415

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
  • hyperbolechi
  • 2015年04月15日 19:11
  • 900

[leetcode][tree] Binary Tree Right Side View

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values o...
  • Clairezz_
  • Clairezz_
  • 2015年05月23日 15:39
  • 174
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Leetcode]Binary Tree Right Side View(TAT)
举报原因:
原因补充:

(最多只允许输入30个字)