[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 ...
随机推荐
- shell脚本函数
不调用就不执行 调用就执行 调用时候的$1是指执行时候的参数1 调用之后的$是位置参数
- uva-10129-欧拉通路
题意:每一个单词的长度最小2,最大1000,单词开头的字母和另外一个单词的末尾一样就可以连接起来,解所有的单词是不是都可以连接起来,没有遗漏的 把每一个单词的第一个字母当成一个结点,最后一个单词也作为 ...
- 练手nginx反向代理和负载均衡apache实战
先说下原理性的 什么是反向代理 用户访问域名 域名的指向到nginx nginx把请求转发到apache apache处理后 返回给用户 整套的逻辑 对于用户来说 就是访问域名 然后返回 没 ...
- 0_Simple__simpleStreams
对比使用单流和多流(4条)情况下数据拷贝,以及数据拷贝加内核调用的效率差别.▶ 源代码 #include <stdio.h> #include <cuda_runtime.h> ...
- leetcode179
class Solution { public: string largestNumber(vector<int>& nums) { int n=nums.size(); vect ...
- JDK1.7之后switch支持string
转自:https://blog.csdn.net/tjcyjd/article/details/9666035 在Java7之前,switch只能支持 byte.short.char.int或者其对应 ...
- 未能同步 iPhone XXX,因为这台电脑不再被授权使用在此iPhone上购买的项目。
打包生成的ipa文件,安装到手机上,p12和ppf证书都正确,手机的udid也正确.用itunes安装到手机报错. 未能同步 iPhone XXX,因为这台电脑不再被授权使用在此iPhone上购买的项 ...
- hibernate-注解及配置
联合主键: 一.将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将该类注解为@Embeddable,最后在主类中(该类不 ...
- 判断一个对象是否为真 __nonzero__ 方法和 __len__方法
class A(): def __nonzero__(self): # 判断 一个对象是否为空,先查看该方法的返回值 return 1 def __len__(self): # 如果没有上一个方法,那 ...
- Nunit与Vs 2012配合使用
要使用Nunit首先要去官网 http://www.nunit.org/ 下载Nunit.win .msi是安装版. bin .zip是绿色版. 下载完后安装. 在 VS2012 中使用 Nu ...