LeetCode No.199 Binary Tree Right Side View
来源:互联网 发布:战狼2知乎 编辑:程序博客网 时间:2024/06/04 18:46
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]
.
====================================================================================
题目链接:https://leetcode.com/problems/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) {} * }; */class Solution {public: vector<int> rightSideView(TreeNode* root) { vector <int> ans ; if ( root == NULL ) return ans ; queue < pair <TreeNode*,int> > q ; q.push ( make_pair ( root , 0 ) ) ; while ( !q.empty() ) { pair <TreeNode*,int> node = q.front() ; q.pop() ; TreeNode* temp = node.first ; int index = node.second ; if ( ans.size() <= index ) ans.push_back ( 0 ) ; ans[index] = temp -> val ; if ( temp -> left ) q.push ( make_pair ( temp -> left , index + 1 ) ) ; if ( temp -> right ) q.push ( make_pair ( temp -> right , index + 1 ) ) ; } return ans ; }};
0 0
- Leetcode NO.199 Binary Tree Right Side View
- leetCode No.199 Binary Tree Right Side View
- LeetCode No.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
- Leetcode #199 Binary Tree Right Side View
- LeetCode 199 Binary Tree Right Side View
- 红茶一杯话Binder(传输机制篇_上)
- c++初学
- 【转载】Java多线程编程2--同步锁定--死锁
- 红茶一杯话Binder(传输机制篇_中)
- 红茶一杯话Binder(传输机制篇_下)
- LeetCode No.199 Binary Tree Right Side View
- //cpp 8.8题解
- Linux下使用备份
- Leetcode139: Word Break
- 约瑟夫问题
- 红茶一杯话Binder(ServiceManager篇)
- NDK开发学习笔记—C/C++访问java成员
- 【转】SimpleDateFormat 时间处理错误等奇葩原因 非线程安全处理
- 深入讲解Android Property机制