[抄题]:

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 从右侧看的节点数的更多相关文章

  1. [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 ...

  2. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  3. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  4. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  5. 【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, ...

  6. 【刷题-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, ...

  7. [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 ...

  8. 199 Binary Tree Right Side View 二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树,   1            <--- /   \2     3         <--- \  ...

  9. 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 ...

随机推荐

  1. 第二个web网页

    1.源代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8">-------- ...

  2. zombodb 聚合函数

    zombodb 暴露基本上所有es 的集合函数为sql 函数,我们可以方便使用 比如 count FUNCTION zdb.count( index regclass, query zdbquery) ...

  3. kvm报错集

    虚拟机console窗口看到一些报错 也可以在终端使用dmesg命令查看 [17617.701174] kvm [17393]: vcpu0 unhandled rdmsr: 0x1ad [19053 ...

  4. ClientDataSet

    TField对象的SetText和GetText事件处理函数 使用TField对象的SetText和GetText事件处理函数可方便的解决字段的代码与代码所对应值的显示问题 TSimpleDatase ...

  5. SQL 语句 explain 分析

      分析索引的效率: > EXPLAIN sql; EXPLAIN 分析的结果的表头如下: id | select_type | table | partitions | type | poss ...

  6. U3D学习12-黑暗之光实例

    1.static勾选后,在scene场景操作后,导致不断烘焙,cpu占用高? 取消自动烘焙 2.UI操作事件 //监听事件增加         mainInputField.onValueChange ...

  7. win10安装git

    Git官网下载:https://git-scm.com/download 选择win系统,下载.然后选择安装路径,一路next.安装成功后 打开cmd 输入 git --version 配置信息 设置 ...

  8. node项目初始化的一些配置

    1. const port = process.env.PORT || 9001; 本地开发用9001端口 2. package.json中配置几个启动命令 "scripts": ...

  9. Bootstrap关闭当前页

       function doBack() {        var index = parent.layer.getFrameIndex(window.name);        parent.lay ...

  10. 关于element-ui日期选择器disabledDate使用心得

    实现目的: 使用type="data"类型实现具备开始日期与结束日期组件(ps:element有自带的type="daterange"类型的组件可以实现此功能) ...