Skip to content

Latest commit

 

History

History
67 lines (53 loc) · 1.68 KB

199-binary-tree-right-side-view.md

File metadata and controls

67 lines (53 loc) · 1.68 KB

199. Binary Tree Right Side View - 二叉树的右视图

给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

示例:

输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---

题目标签:Tree / Depth-first Search / Breadth-first Search

题目链接:LeetCode / LeetCode中国

题解

Language Runtime Memory
cpp 4 ms 856.1 KB
/**
 * 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;
        if (!root) return res;
        queue<TreeNode*> Q;
        Q.push(root);
        while (!Q.empty()) {
            int n = Q.size();
            while (n--) {
                auto t = Q.front();
                if (n == 0) res.push_back(t->val);
                Q.pop();
                if (t->left) Q.push(t->left);
                if (t->right) Q.push(t->right);
            }
        }
        return res;
    }
};
static auto _ = [](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();