[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]多播代理
1. 第一种 DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FReceiveDelegateEvent, FString, Value1, FString, ...
- div+Css绝对定位(absolute)和相对定位(relative)的总结
1.没有外Div的情况下 设置绝对定位(absolute)和相对定位(relative)是没有区别的 2.相对定位占位置 而绝对定位不占位置 会漂浮在别的Div之上 3.若父Div没有设置定位,子Di ...
- !!! jquery mobile常用代码
Jquery MOBILE: (2014-7-1 发布jquery.mobile 1.4.3版本) <!doctype html> <html> <head> & ...
- 6. 纯 CSS 绘制一颗闪闪发光的璀璨钻石
原文地址:https://segmentfault.com/a/1190000014652116 HTML代码: <div class="diamond"> <s ...
- 二维数组 cudaMallocPitch() 和三维数组 cudaMalloc3D() 的使用
▶ 使用函数 cudaMallocPitch() 和配套的函数 cudaMemcpy2D() 来使用二维数组.C 中二维数组内存分配是转化为一维数组,连贯紧凑,每次访问数组中的元素都必须从数组首元素开 ...
- 使用three.js开发3d地图初探
three是图形引擎,而web二维三维地图都是基于图形引擎的,所以拿three来开发需求简单的三维地图应用是没什么问题的. 1.坐标转换 实际地理坐标为经度.纬度.高度,而three.js使用的是右手 ...
- 关于RabbitMQ以及RabbitMQ和Spring的整合
转自:https://www.cnblogs.com/s648667069/p/6401463.html 基本概念 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是 ...
- Tomcat Servlet学习
参照: 浅谈cookie跨域的解决方案——document.domain(http://blog.csdn.net/zhouziyu2011/article/details/61200943) Ser ...
- C#连接sqlserver windows 和 sqlserver 身份验证的两种连接字符串
//sql server 身份验证 连接字符串 private string ConnstrSqlServer = "server=服务器名称;uid=登录名称;pwd=登录密码;datab ...
- a,b = b,a 换值问题
a = "hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhe ...