C/C++编程题刷题:leetcode 199. 二叉树的右视图
2020-08-23 10:04
141 查看
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:
[code]输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 <--- / \ 2 3 <--- \ \ 5 4 <---
[code]/** * 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) { if(!root) return {}; queue<TreeNode*> q; q.push(root); q.push(nullptr); vector<int> res; int tmp; while(q.size()){ auto t = q.front(); q.pop(); if(t){ tmp = t->val; if(t->left) q.push(t->left); if(t->right) q.push(t->right); } else{ res.push_back(tmp); if(q.size()) q.push(nullptr); } } return res; } };
相关文章推荐
- C++Leetcode199:二叉树的右视图
- leetcode-199. 二叉树的右视图
- C/C++编程题刷题:leetcode 104. 二叉树的最大深度
- C/C++编程题刷题:leetcode 543. 二叉树的直径
- C/C++编程题刷题:leetcode 958. 二叉树的完全性检验
- leetcode-199-二叉树的右视图
- C/C++编程题刷题:leetcode 814. 二叉树剪枝
- LeetCode199. 二叉树的右视图
- LeetCode 199.二叉树的右视图(dfs,如果是数组实现树)
- [LeetCode题解] 199. 二叉树的右视图
- leetcode199. 二叉树的右视图(bfs)
- Leetcode 199 Binary Tree Right Side View 二叉树右视图
- LeetCode(199):二叉树的右视图
- Leetcode 199 Binary Tree Right Side View 二叉树右视图
- 利用python 完成 leetcode 199 二叉树的右视图
- leetcode199_二叉树的右视图
- 【LeetCode-cpp】【55】199. 中等 二叉树的右视图 Binary Tree Right Side View
- Leetcode 199. 二叉树的右视图
- 【Leetcode】199. 二叉树的右视图
- [和小菜鸡一起刷题(python)] LeetCode 199. 二叉树的右视图. (Binary Tree Right Side View)