C++实现LeetCode(199.二叉树的右侧视图)
[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 4 <---
You should return [1, 3, 4].
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
这道题要求我们打印出二叉树每一行最右边的一个数字,实际上是求二叉树层序遍历的一种变形,我们只需要保存每一层最右边的数字即可,可以参考我之前的博客 Binary Tree Level Order Traversal 二叉树层序遍历,这道题只要在之前那道题上稍加修改即可得到结果,还是需要用到数据结构队列queue,遍历每层的节点时,把下一层的节点都存入到queue中,每当开始新一层节点的遍历之前,先把新一层最后一个节点值存到结果中,代码如下:
/**
* Definition for binary tree
* 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()) {
res.push_back(q.back()->val);
int size = q.size();
for (int i = 0; i < size; ++i) {
TreeNode *node = q.front();
q.pop();
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
return res;
}
};
到此这篇关于C++实现LeetCode(199.二叉树的右侧视图)的文章就介绍到这了,更多相关C++实现二叉树的右侧视图内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
相关推荐
-
C++实现LeetCode(186.翻转字符串中的单词之二)
[LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词之二 Given an input string , reverse the string word by word. Example: Input: ["t","h","e"," ","s","k","y"," ","i&qu
-
C++实现LeetCode(191.位1的个数)
[LeetCode] 191.Number of 1 Bits 位1的个数 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000
-
C++实现LeetCode(188.买卖股票的最佳时间之四)
[LeetCode] 188.Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most k transactions. Note: You m
-
C++实现LeetCode(557.翻转字符串中的单词之三)
[LeetCode] 557.Reverse Words in a String III 翻转字符串中的单词之三 Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode conte
-
C++实现LeetCode(198.打家劫舍)
[LeetCode] 198. House Robber 打家劫舍 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security
-
C++实现LeetCode(309.买股票的最佳时间含冷冻期)
[LeetCode] 309.Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as
-
C++实现LeetCode(190.颠倒二进制位)
[LeetCode] 190. Reverse Bits 颠倒二进制位 Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 Output: 00111001011110000010100101000000 Explanation: The input binary string 00000010100101000001111010011100 re
-
C++实现LeetCode(199.二叉树的右侧视图)
[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
-
C++实现LeetCode(144.二叉树的先序遍历)
[LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历 Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,2,3] Follow up: Recursive solution is trivial, could you do it iterat
-
C++实现LeetCode(94.二叉树的中序遍历)
[LeetCode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历 Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] Follow up: Recursive solution is trivial, could you do it iteratively
-
C++实现LeetCode(112.二叉树的路径和)
[LeetCode] 112. Path Sum 二叉树的路径和 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node with no children. Example: Given the below bi
-
C++实现LeetCode(113.二叉树路径之和之二)
[LeetCode] 113. 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. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7
-
C++实现LeetCode(145.二叉树的后序遍历)
[LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you d
-
C++实现LeetCode(104.二叉树的最大深度)
[LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Note: A leaf is a node with no child
-
C++实现LeetCode(103.二叉树的之字形层序遍历)
[LeetCode] 103. 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). For example
-
C++实现LeetCode(107.二叉树层序遍历之二)
[LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历之二 Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf to root). Example 1: Input: root = [3
-
C++实现LeetCode(102.二叉树层序遍历)
[LeetCode] 102. 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). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15
随机推荐
- JQUERY复选框CHECKBOX全选,取消全选
- Bootstrap3制作搜索框样式的方法
- Java 四种基本加密算法分析
- jQuery 瀑布流 浮动布局(一)(延迟AJAX加载图片)
- Visual Studio 2017 针对移动开发的新特性汇总
- 将CMYK颜色值和RGB颜色相互转换的PHP代码
- Android开发之毛玻璃效果实例代码
- JavaScript中的关键字"VAR"使用详解 分享
- android app进行代码混淆实例详解
- Ajax 汇总以及初步评价
- Css浏览器兼容的解决方法
- JQuery操作tr和td内容的方法实例
- 金山反病毒20050104_日报
- jQuery 菜单随滚条改为以定位方式(固定要浏览器顶部)
- Jtable和JTree的写法示例代码
- 常用的java日期比较和日期计算方法小结
- ASP.NET项目开发中日期控件DatePicker如何使用
- sort page 排序和分页的小例子
- 微信小程序页面跳转功能之从列表的item项跳转到下一个页面的方法
- Spring MVC学习教程之视图深入解析
