博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
199. 二叉树的右视图
阅读量:7239 次
发布时间:2019-06-29

本文共 979 字,大约阅读时间需要 3 分钟。

给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

示例:

输入: [1,2,3,null,5,null,4]输出: [1, 3, 4]解释:   1            <--- /   \2     3         <--- \     \  5     4       <---

 

class Solution {    public List
rightSideView(TreeNode root) { List
res = new ArrayList<>(); Queue
queue = new LinkedList<>(); if(root == null) return res; queue.add(root); while(!queue.isEmpty()){ int size = queue.size(); for(int i = 0; i < size; i++){ TreeNode node = queue.poll(); if(i == size - 1) res.add(node.val); if(node.left != null) queue.add(node.left); if(node.right != null) queue.add(node.right); } } return res; }}
执行用时 : 
3 ms, 在Binary Tree Right Side View的Java提交中击败了51.57% 的用户
内存消耗 : 
34.6 MB, 在Binary Tree Right Side View的Java提交中击败了0.00% 的用户

转载于:https://www.cnblogs.com/Roni-i/p/10509910.html

你可能感兴趣的文章
37、pendingIntent 点击通知栏进入页面
查看>>
TCP为何采用三次握手来建立连接,若采用二次握手可以吗?
查看>>
Jfreet 自动删除生成的图片
查看>>
snmp
查看>>
java笔记----java新建生成用户定义注释
查看>>
批量删除记录时如何实现全选【总结】
查看>>
Thread’s start method and run method
查看>>
使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【二】——使用Repository模式构建数据库访问层...
查看>>
CDN发展史
查看>>
Atitit.研发团队的管理原则---立长不立贤与按资排辈原则
查看>>
UVa 10763 - Foreign Exchange
查看>>
#lspci | grep Eth
查看>>
日订单峰值破40万!58速运订单调度系统架构大解密
查看>>
Objective-C 资源收藏
查看>>
MFC——从实现角度分析微云界面
查看>>
正则 群组 Group
查看>>
An Introduction To The SQLite C/C++ Interface
查看>>
关闭Pycharm拼写检查
查看>>
一个优秀的程序员是如何炼成的?
查看>>
区块链的12个技术理解误区,你知道哪些?
查看>>