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 ...
随机推荐
- .htaccess FollowSymlinks影响rewrite功能
Thinkphp的框架的根目录的.htaccess是这样写的: <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine ...
- Dart 学习资料
Dart 学习资料: 学习资料 网址 Dart 编程语言中文网 http://dart.goodev.org/ Dart 官方包仓库 https://pub.dartlang.org/ 你想了解的Da ...
- 在 sql 语句出现 warning 之后,立刻执行 `show warnings;` 就可以看到 warning 提示信息
在 sql 语句出现 warning 之后,立刻执行 show warnings; 就可以看到 warning 提示信息
- 全面理解Java内存模型(JMM)及volatile关键字
[版权申明]未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/72772461 出自[zejian ...
- 如何在同一主机中添加多个homestead并行开发
参考源 https://blog.csdn.net/smm188/article/details/79356150 1,在项目目录 git clone homestead 后(见上面流程中的第四步), ...
- FPGA中iic总线上,应答ACK解析
首先要明白一点,有效ACK是指第9位为低电平,第十位,十一位就管不着了,(我写的代码发现第九位为低电平,之后复位为高电平,开始没注意后来搞的很是头痛) 主机发ack和主机检测ack,主机发ack是在从 ...
- redis高可用(哨兵机制)
redis哨兵机制:redis的哨兵系统用于管理多个reids服务器,该系统主要有三个作用: 监控:哨兵 会不断地检查你的主服务(Master)和从服务器(Slave)是否运作正常. 提醒:当被监控的 ...
- word文档内容如何防止被复制
word2016 审阅->限制编辑->1格式设置编辑 and 2编辑限制->3是,启动强制保护->输入秘密
- Java异常学习总结一
Java中的异常 定义 异常(Exception)就是在程序的运行过程中所发生的不正常的事件,它会中断正在运行的程序. 常见类型举例 所需文件找不到(ClassNotFoundException) 网 ...
- 使用 JavaScript 将 XML 转成 JSON
function xmlToJson(xml) { // Create the return object var obj = {}; if (xml.nodeType == 1) { // elem ...