【LEETCODE】199-Binary Tree Right Side View
2015-11-05 21:27
309 查看
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.
For example:
Given the following binary tree,
1 <---
/ \
2 3 <---
\ \
5 4 <---
You should return [1, 3, 4].
right side of it, return the values of the nodes you can see ordered from top to bottom.
For example:
Given the following binary tree,
1 <---
/ \
2 3 <---
\ \
5 4 <---
You should return [1, 3, 4].
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def rightSideView(self, root): """ :type root: TreeNode :rtype: List[int] """ if root is None: return [] result=[] cur=[root] while cur: nextlevel=[] #存放下一层的node for i,node in enumerate(cur): #当前层的 所有节点node 和其序号 if node.left: nextlevel.append(node.left) #nextlevel先记录left child,再记录right child if node.right: nextlevel.append(node.right) if i==len(cur)-1: #如果当前node就是当前层的最右点,则append到result里 result.append(node.val) cur=nextlevel #循环完当前层的node后,cur进入下一层 return result
相关文章推荐
- _MSC_VER详细介绍
- 汇编3个字节型变量倒序输出
- Java IAQ:很少被回答的问题
- 翻译随笔(2)——摆脱首份工作综合征
- js setTimeout 和 setInterval实现多线程
- 每个程序员都会的 35 个 jQuery 小技巧
- Openjudge-NOI题库-出书最多
- 测试博客园Markdown编辑器
- 遥感影像数据产品级别概述
- [Leetcode]Clone Graph
- Openstack常用命令
- Linux文件传输和vsftpd服务器
- iOS闹钟实现
- POJ 3259 Wormholes bellman-ford判负环
- 安装Ubuntu 15 Kylin 网络线口不亮 连不上网 网络驱动更换[终极解决办法]
- 数据存储容量单位转换
- [java] 发一个企业人员管理系统的代码
- 关于写博客这件事
- 借助 AOP 为 Java Web 应用记录性能数据
- CodeForces 405C