博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
199. Binary Tree Right Side View
阅读量:7050 次
发布时间:2019-06-28

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

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       <---

难度: medium

题目: 给定二叉树,想像一下你站在树的右边,返回能看到的所有结点,结点从上到下输出。

思路:层次遍历,BFS

Runtime: 1 ms, faster than 79.74% of Java online submissions for Binary Tree Right Side View.

Memory Usage: 34.7 MB, less than 100.00% of Java online submissions for Binary Tree Right Side View.

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public List
rightSideView(TreeNode root) { List
result = new ArrayList<>(); if (null == root) { return result; } Queue
queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { int qSize = queue.size(); for (int i = 0; i < qSize; i++) { TreeNode node = queue.poll(); if (node.right != null) { queue.add(node.right); } if (node.left != null) { queue.add(node.left); } if (0 == i) { result.add(node.val); } } } return result; }}

转载地址:http://zgpol.baihongyu.com/

你可能感兴趣的文章
HDU-1045 Fire NetFire Net 最大团
查看>>
Eclipse自动部署项目到Tomcat的webapps下的有效方法
查看>>
Extjs4快速上手三——实现主界面
查看>>
如何替代Flex?
查看>>
控制属性修改时间,控制时间,联合主键
查看>>
android-包签名
查看>>
Android开发和调试必备工具-SDK Tools
查看>>
《微软的软件测试之道》读书笔记 之 结构测试技术
查看>>
ASP.NET中Session的个人浅谈
查看>>
Ubuntu离线安装Sogou拼音(附老版本安装&输入法自启动)
查看>>
AS3学习笔记(三)XML解析
查看>>
cisco asa 5520 8.4 (二) -- 动态nat
查看>>
java.io.NotSerializableException
查看>>
php中instanceof的作用
查看>>
启用客服qq的方法
查看>>
Mac OS X Lion 10.7.3 发布
查看>>
Freiburg这么做太愚蠢了
查看>>
Vue+Vue Router+Axios+Webpack+Flask+MySQL实现简单的注册、登录验证功能
查看>>
Spring Cache
查看>>
基于Nginx和Memcache的负载均衡集群架构设计
查看>>