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 <---
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
知道是层遍历三部曲,不知道怎么变形
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
先左后右,第一个在左
先右后左,第一个在右
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
先左后右,第一个在左
先右后左,第一个在右
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
//ini
List<Integer> res = new ArrayList<Integer>(); //cc
if (root == null) return res; //bfs in 3 steps
Queue<TreeNode> q = new LinkedList<>(); q.offer(root);
while (!q.isEmpty()) {
int size = q.size(); for (int i = 0; i < size; i++) {
TreeNode cur = q.poll();
System.out.println("cur.val = " + cur.val);
//add previously if it is from the last row
if (i == 0) res.add(cur.val); if (cur.right != null) q.offer(cur.right);
if (cur.left != null) q.offer(cur.left);
}
} //return
return res;
}
}
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 、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
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】199. Binary Tree Right Side View
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
- 【刷题-LeetCode】199 Binary Tree Right Side View
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
- [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 ...
- 199 Binary Tree Right Side View 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树, 1 <--- / \2 3 <--- \ ...
- 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 ...
随机推荐
- zabbix图形化界面乱码(二)
中文字体乱码,解决办法: 1:从Windos下拷贝字体到服务器,C:\Windows\Fonts,有很多,看着喜欢的拷贝 2:然后在zabbix 服务端,进入到zabbix web的工作目 ...
- python3.6 内置函数
python内置函数 # encoding: utf-8 # module builtins # from (built-in) # by generator 1.145 ""&q ...
- python之路——8
王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 学习内容 .1.文件操作 笔记.txt 1.文件路径:D:\python\Day8\笔记.txt 2.编码方 ...
- iOS ReactiveCocoa的使用
一.ReactiveCocoa简介 reactiveCocoa简称RAC,它是一个三方框架,很多人把它叫做函数响应式编程框架,因为它具有函数式编程和响应式编程的特性. 由于该框架的编程思想,使得它具有 ...
- .net 连接 Oracle 可能需要配置
D:\Program Files (x86)\Oracle Developer Tools for VS2013\network\admin\tnsnames.ora
- 知识点:图说 Mysql 权限管理
图: #授权表 user #该表放行的权限,针对:所有数据,所有库下所有表,以及表下的所有字段 db #该表放行的权限,针对:某一数据库,该数据库下的所有表,以及表下的所有字段 tables_priv ...
- java的String的乱码浅析
Java又乱码了,怎么办:乱码了说明编码与解码不一致导致.所以使用统一的编码方式即可. 本文并不是一定能解决乱码,本文主要用来了解jvm默认编码,以及string编码与解码一致性问题. jvm的默认编 ...
- JS高级-异步
单线程 只有一个线程,同一时间只能做一件事 原因:避免DOM渲染的冲突 浏览器需要渲染DOM JS可以修改DOM结果 JS执行的时候,浏览器DOM渲染会暂停 两段JS也不能同时执行(修改DOM就冲突) ...
- SQLServer数据库镜像配置
目录 一.目标...2 二.前提条件.限制和建议...2 三.设置概述...2 四.安装Sql Server 2008 enterprise X64.3 4.1.安装.NET3.5.3 4.2.安装时 ...
- 多线程之sleep和wait的区别
它们最大本质的区别是:sleep()不释放同步锁,wait()释放同步锁. 还有用法的上的不同是:sleep(milliseconds)可以用时间指定来使他自动醒过来,如果时间不到你只能调用inter ...