Leetcode 199 Binary Tree Right Side View 二叉树右视图
来源:互联网 发布:大数据分析技术 编辑:程序博客网 时间:2024/06/17 01:54
原题地址
https://leetcode.com/problems/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].
你需要返回[1, 3, 4].
解题思路
首先可以明确的是,看到的结点是每一层的结点中最右侧的结点,我们要做的就是按照层序把每一层最右侧的结点找出来并返回.然后针对按照层序的关键点,我们可以考虑层序遍历的类似思路.层序遍历中需要用到队列,这里我们考虑也使用队列.但是与层序遍历不同的是,我们需要非常明确的区分开不同的层,以保存每层的最后一个结点.我们可以使用两个队列,一个队列中存储某一层的结点,另一个队列存储下一层的结点,这样做到把不同层的结点区分开来.
原题地址
https://leetcode.com/problems/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].
你需要返回[1, 3, 4].
解题思路
首先可以明确的是,看到的结点是每一层的结点中最右侧的结点,我们要做的就是按照层序把每一层最右侧的结点找出来并返回.然后针对按照层序的关键点,我们可以考虑层序遍历的类似思路.层序遍历中需要用到队列,这里我们考虑也使用队列.但是与层序遍历不同的是,我们需要非常明确的区分开不同的层,以保存每层的最后一个结点.我们可以使用两个队列,一个队列中存储某一层的结点,另一个队列存储下一层的结点,这样做到把不同层的结点区分开来.
代码 cpp
class Solution {public: /** 获取每一层最右侧的结点 */ vector<int> rightSideView(TreeNode* root) { /* 两个队列交替使用,获取队尾的元素 */ vector<int> ret; queue<TreeNode*> queues[2]; /* 初始化,把第一层结点放入第一个队列*/ if (root != NULL) queues[0].push(root); /* 交替使用队列,一个队列存储一层结点 * 另一个队列存储下一层结点(或上一层结点) */ int i = 0, j = 1, tmp; TreeNode *p; while (!queues[0].empty() || !queues[1].empty()) { while (!queues[i].empty()) { p = queues[i].front(); queues[i].pop(); if (p->left) queues[j].push(p->left); if (p->right) queues[j].push(p->right); tmp = p->val; } // 遍历完某一层结点时,tmp是该层最右侧的结点的值 ret.push_back(tmp); // 添加到结果集 i = (i + 1) % 2; // 交换两个队列的角色 j = (j + 1) % 2; } return ret; }};
- Leetcode 199 Binary Tree Right Side View 二叉树右视图
- Leetcode 199 Binary Tree Right Side View 二叉树右视图
- Binary Tree Right Side View 二叉树:右视图
- leetcode 199. Binary Tree Right Side View-树的右视图|递归|非递归|Java|Python
- LeetCode 199. Binary Tree Right Side View(二叉树右侧视图)
- [leetcode] 199 Binary Tree Right Side View
- leetcode-199 Binary Tree Right Side View
- 199Binary Tree Right Side View-LeetCode
- [LeetCode 199]Binary Tree Right Side View
- Leetcode 199: Binary Tree Right Side View
- LeetCode 199-Binary Tree Right Side View
- Binary Tree Right Side View - LeetCode 199
- leetcode[199]:Binary Tree Right Side View
- 【Leetcode】Binary Tree Right Side View #199
- LeetCode(199)Binary Tree Right Side View
- [LeetCode 199] Binary Tree Right Side View
- leetcode 199: Binary Tree Right Side View
- 【LEETCODE】199-Binary Tree Right Side View
- 元器件小积累
- adb logcat 查看日志
- Git学习笔记
- QT5入门之24 -QT 容器
- django signal 拾遗
- Leetcode 199 Binary Tree Right Side View 二叉树右视图
- Java Collections.addAll() 与 ArrayList.addAll() 的区别
- 与幼儿的沟通
- 解读:在命令情况下使用Android NDK交叉编译工具
- 使用Objective-C的文档生成工具:appledoc
- dajngo.forms.widget-Media
- spring 文件上传
- Server2008R2:由于没有远程桌面授权服务器可以提供许可证,.....错误的解决
- MySQL Replication, 主从和双主配置