[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 ...
随机推荐
- [UE4]动画事件
在动画中添加事件通知,在动画蓝图中就可以使用这个事件通知: 在动画蓝图中可以使用“Try Get Pawn Owner”取得控制的角色实例 在Controller中,可以使用“Get Controll ...
- [UE4]多播代理
1. 第一种 DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FReceiveDelegateEvent, FString, Value1, FString, ...
- PHP 算术运算符
PHP 算术运算符 运算符 名称 描述 实例 结果 x + y 加 x 和 y 的和 2 + 2 4 x - y 减 x 和 y 的差 5 - 2 3 x * y 乘 x 和 y 的积 5 * 2 1 ...
- diskspd的使用
参数翻译 可测试目标: file_path 文件abc.file #<physical drive number> #1为第一块物理磁盘[谨慎,别拿系统盘测试,一般用于准备投入的数据磁盘测 ...
- 经典算法 BFPRT算法详解
内容: 1.原始问题 => O(N*logN) 2.BFPRT算法 => O(N) 1.原始问题 问题描述:给你一个整型数组,返回其中第K小的数 普通解法: 这道题可以利用 ...
- tornado-模板,转义,上传静态文件
class MainHandler(tornado.web.RequestHandler): def get(self): self.render("ces.html") def ...
- Latex Error:‘acmart.cls’ not found 解决方案:
windows下latex编译ACM论文模板时,出现Latex Error:‘acmart.cls’ not found,解决方案: 首先cd至模板所在目录下,然后运行以下命令: tex acma ...
- phpExcel中文帮助手册
phpExcel中文帮助手册 Admin 2011年11月13日 名人名言:上人生的旅途吧.前途很远,也很暗.然而不要怕.不怕的人的面前才有路.——有岛武郎 下面是总结的几个应用办法 include ...
- C# EF 基础操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- UI5-文档-4.36-Device Adaptation
现在,我们根据运行应用程序的设备配置控件的可见性和属性.通过使用sap.ui.设备API和定义一个设备模型,我们将使应用程序在许多设备上看起来很棒. Preview On phone devices, ...