(二叉树 bfs) 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.
Example:
- Input: [1,2,3,null,5,null,4]
- Output: [1, 3, 4]
- Explanation:
- 1 <---
- / \
- 2 3 <---
- \ \
- 5 4 <---
--------------------------------------------------------------------------------------------------
水。。。。。。用bfs就可以快速先解决掉了- C++代码:
- /**
- * 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> vec;
- if(!root) return vec;
- queue<TreeNode*> q;
- q.push(root);
- while(!q.empty()){
- for(int i = q.size(); i > ; i--){
- auto t = q.front();
- q.pop();
- if(i == ){
- vec.push_back(t->val);
- }
- if(t->left) q.push(t->left);
- if(t->right) q.push(t->right);
- }
- }
- return vec;
- }
- };
(二叉树 bfs) leetcode 199. Binary Tree Right Side View的更多相关文章
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [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 nod ...
- [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 nod ...
- leetcode@ [199] Binary Tree Right Side View (DFS/BFS)
https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...
- (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [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 nod ...
- Java for 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 nod ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...
随机推荐
- jaxp的dom方式操作(查找、添加、修改、删除、遍历节点)
package cn.itcast.jaxptest; import java.io.IOException; import javax.xml.parsers.DocumentBuilder;imp ...
- Razor Pages with ASP.NET Core 2
With ASP.NET Core 2 we get another way of building web applications. It’s one of those new things th ...
- Codevs1541[USACO]围墙涂色
离散加差分有点涨姿势啊 对我这种菜鸡而言还是第一次看到啊qwq 题面 大意 :n次,每次覆盖一个区间,求覆盖过m次的节点个数 sol:大概是差分的思想加上离散,就可以解决普通差分无法解决的问题了,比如 ...
- THEPYTHONCHALLENG闯关记录
由于是自己看视频学python,总觉得不写几行代码就什么都没有学到. 找了一个写代码的网站其实只是因为这个看起来好玩. 闯关地址http://www.pythonchallenge.com/index ...
- avpicture_fill的实现
简介 avpicture_fill函数将ptr指向的数据填充到picture内,但并没有拷贝,只是将picture结构内的data指针指向了ptr的数据.其实现如下: avpiture_fill av ...
- git 回退版本
回滚到指定的版本 git reset --hard e377f60e28c8b84158 强制提交 git push -f origin master
- docker --环境变量制作模板
比如你现在有配置文件:decoder.conf decoder.conf里面有两个配置项写的是: THREAD_NUM:24 GPU_ID:0 现在你希望通过某个统一的配置来修改这些配置项,以免在部署 ...
- python 脚本之 IP地址探测
#第一种方法#!/usr/bin/env python #_*_ coding:utf8 _*_ #### 该脚本需要使用fping命令 如果没有安装需要提前安装fping #### yum inst ...
- 谈谈IE针对Ajax请求结果的缓存
在默认情况下,IE会针对请求地址缓存Ajax请求的结果.换句话说,在缓存过期之前,针对相同地址发起的多个Ajax请求,只有第一次会真正发送到服务端.在某些情况下,这种默认的缓存机制并不是我们希望的(比 ...
- 执行sql脚本保留操作日志
需求场景,操作数据库场景较多,无专业dba,腾讯云mysql虽然提供了类似于phpmyadmin的管理后台,但是操作卡,效率低 #!/usr/bin CDATE=`date +%Y%m%d-%H%M% ...