Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >LeetCode 0199 - Binary Tree Right Side View

LeetCode 0199 - Binary Tree Right Side View

作者头像
Reck Zhang
发布于 2021-08-11 06:59:09
发布于 2021-08-11 06:59:09
26800
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Binary Tree Right Side View

Desicription

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.

Example:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:

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

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * 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 {
private:
    void fun(vector<int>& res, int level, TreeNode* root) {
        if(root == NULL)
            return ;
        if(res.size() < level)
            res.push_back(root->val);
        fun(res, level+1, root->right);
        fun(res, level+1, root->left);
    }
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> res;
        fun(res, 1, root);
        return res;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-06-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
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 nodes you can see ordered from top to bottom. For example: Given the following binary tree, 1 <--- / \ 2 3 <--- \ \ 5
triplebee
2018/01/12
4630
Tree - 199. 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.
ppxai
2020/09/23
2960
LeetCode 0107 - Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).
Reck Zhang
2021/08/11
1570
LeetCode 0103 - Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).
Reck Zhang
2021/08/11
1640
LeetCode 0102 - Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).
Reck Zhang
2021/08/11
1900
Baozi Training Leetcode solution 199:Binary Tree Right Side View
Leetcode solution 199:Binary Tree Right Side View
包子面试培训
2019/10/12
4250
Baozi Training Leetcode solution 199:Binary Tree Right Side View
Leetcode: 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.
卡尔曼和玻尔兹曼谁曼
2019/01/22
3600
LeetCode 0257 - Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
Reck Zhang
2021/08/11
2350
Leetcode 199. Binary Tree Right Side View
版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/82791672
Tyan
2019/05/25
2640
LeetCode 0104 - Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
Reck Zhang
2021/08/11
1560
LeetCode 0094 - Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes’ values.
Reck Zhang
2021/08/11
1660
【leetcode刷题】T128-二叉树的右视图
https://leetcode-cn.com/problems/binary-tree-right-side-view/
木又AI帮
2019/07/30
4790
LeetCode 0095 - Unique Binary Search Trees II
Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1…n.
Reck Zhang
2021/08/11
1720
Leetcode 144 Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. 二叉树先序遍历,easy /** * Definition for a binary tree node. * struct TreeNode { * int v
triplebee
2018/01/12
6680
Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 求二叉树所有从根到叶的路径。 简单DFS /** * Definition for a binary tree node. * struct TreeN
triplebee
2018/01/12
5060
LeetCode 0111 - Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.
Reck Zhang
2021/08/11
1840
LeetCode 199. Binary Tree Right Side View
题目 题意:假如你在一棵二叉树的右边,往左看,能看到哪些元素。 题解:广搜,每一层的最右边元素即可。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ struct No
ShenduCC
2020/02/19
2960
LeetCode 0235 - Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
Reck Zhang
2021/08/11
2330
LeetCode 0236 - Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
Reck Zhang
2021/08/11
1750
LeetCode 0113 - Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.
Reck Zhang
2021/08/11
2340
相关推荐
Leetcode 199 Binary Tree Right Side View
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文