[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-树的右视图|递归|非递归|Java|Python

原题链接:199. Binary Tree Right Side View 【思路】非递归实现 层次遍历,每一层都是从左到右遍历,将每一层的最右一个节点添加到结果集中就是我们需要得到的结果: publ...

LeetCode: Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod...

LeetCode #199 - Binary Tree Right Side View

题目描述: Given a binary tree, imagine yourself standing on the right side of it, return the values of ...
  • LawFile
  • LawFile
  • 2016年11月03日 23:17
  • 93

Leetcode: Binary Tree Right Side View

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of...

LeetCode Binary Tree Right Side View

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values o...

LeetCode-199. Binary Tree Right Side View (JAVA)(二叉树最右侧结点)

LeetCode-199. Binary Tree Right Side View (JAVA)(二叉树最右侧结点)

Leetcode 199 Binary Tree Right Side View 二叉树右视图

给出一个二叉树,设想你从树的右侧看过来,返回你从上往下可以看到的结点....

LeetCode - 199. Binary Tree Right Side View

题目要求写出Binary Tree从右侧看的情况,我们首先可以想到的是基于Level Order Traversal的方法,我们只需要将每一层最右侧的节点加入结果中即可,这种思路的代码如下: pub...

Leetcode 199. Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod...

Binary Tree Right Side View - LeetCode 199

题目描述: Given a binary tree, imagine yourself standing on the right side of it, return the values of ...
  • bu_min
  • bu_min
  • 2015年06月10日 19:26
  • 222
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Leetcode]Binary Tree Right Side View(TAT)
举报原因:
原因补充:

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