[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 <---
思路
DFS
每当recursion一进入到next level,
就立马加上该level的right side node到result里
对应的,
在recursion的时候,先处理 root.right
代码
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
if(root == null) return result;
dfs(root, result, );
return result;
}
private void dfs(TreeNode root, List<Integer> result, int level ){
// base case
if(root == null) return;
/*height == result.size() limits the amount of Node add to the result
making sure that once go to the next level, add right side node to result immediately
*/
if(level == result.size()){
result.add(root.val);
}
// deal with right side first, making sure right side node to be added first
dfs(root.right, result, level+);
dfs(root.left, result, level+);
}
}
思路
BFS(iteration)
每次先将right side node 加入到queue里去
保证 当i = 0 的时候,poll出来的第一个item是right side node
代码
public List<Integer> rightSideView(TreeNode root) {
// level order traversal
List<Integer> result = new ArrayList();
Queue<TreeNode> queue = new LinkedList();
// corner case
if (root == null) return result;
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i< size; i++) {
TreeNode cur = queue.poll();
// make sure only add right side node
if (i == 0) result.add(cur.val);
// add right side node first, making sure poll out first
if (cur.right != null) queue.offer(cur.right);
if (cur.left != null) queue.offer(cur.left);
}
}
return result;
}
[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
// 我的代码 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- (二叉树 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 nod ...
- 199 Binary Tree Right Side View 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树, 1 <--- / \2 3 <--- \ ...
- 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 ...
- 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 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
随机推荐
- SimpleXML概述
要处理XML 文件,有两种传统的处理思路:SAX 和DOM.SAX 基于事件触发机制,对XML 文件进行一次扫描,完成要进行的处理:DOM 则将整个XML 文件构造为一棵DOM树,通过对DOM 树的遍 ...
- pandas的set_index和reset_index方法
import pandas as pd data = pd.DataFrame(np.arange(1,10).reshape(3,3),index=["a","b&qu ...
- javascript cookie操作.
给cookie追加name: document.cookie="name="+escape("我叫钢蛋");//这里可以不写secape,这样写会更加的安全., ...
- kudu架构(转)
特点: High availability(高可用性).Tablet server 和 Master 使用 Raft Consensus Algorithm 来保证节点的高可用,确保只要有一半以上 ...
- Centos 克隆后端口eth1怎么改回eth0
复制或克隆后成功并做好后续问题的虚拟机 修改网卡地址vi /etc/udev/rules.d/70-persistent-net.rules 配置ifcfg-eth0脚本,注意HWADDR那行,要和上 ...
- Introducing Deep Reinforcement
The manuscript of Deep Reinforcement Learning is available now! It makes significant improvements to ...
- ECCV 2018 | Bi-Real net:超XNOR-net 10%的ImageNet分类精度
这项工作由香港科技大学,腾讯 AI lab,以及华中科技大学合作完成,目的是提升二值化卷积神经网络(1-bit CNN)的精度.虽然 1-bit CNN 压缩程度高,但是其当前在大数据集上的分类精度与 ...
- JavaScript中的坑
内容:关于JavaScript中的一些蛋疼的问题以及面试笔试中常见的一些坑爹套路总结 此部分内容持续总结完善中... 1.undefined和null的区别 null: Null类型,代表空值,代表一 ...
- java技术-重点方向
多线程 锁 事务 缓存 hashmap 并发编程
- git常用命令,冲突
使用多个仓库git push cangkuming fenzhiming删除远程仓库 git push 远程仓库名 :删除目标分支 # 需要先删除本地目标分支 git pull <远程主机名&g ...