【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

标签: LeetCode


题目地址:https://leetcode.com/problems/binary-tree-right-side-view/description/

题目描述:

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

题目大意

打印出二叉树每层的最右边的元素。

解题方法

这个题就是102. Binary Tree Level Order Traversal翻版啊!上个题是要直接打印每层的元素,这个是要每层元素的最右边元素,所以可以使用之前的解法,然后再把每层的给取出来嘛~~

递归解法:

# 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]
"""
res = []
self.levelOrder(root, 0, res)
return [level[-1] for level in res] def levelOrder(self, root, level, res):
if not root: return
if len(res) == level: res.append([])
res[level].append(root.val)
if root.left: self.levelOrder(root.left, level + 1, res)
if root.right: self.levelOrder(root.right, level + 1, res)

非递归解法,使用队列。

这个解题的技巧在于,queue其实也可以用[-1]直接找到这个层的最后一个元素。

每次进行while循环,都是开始了新的一层,for循环的巧妙在于,直接遍历队列中已有的元素,也就是上层的元素。这样的话就直接把上层的遍历完了,新的层也加入了队列。

# 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]
"""
res = []
if not root: return res
queue = collections.deque()
queue.append(root)
while queue:
res.append(queue[-1].val)
for i in range(len(queue)):
node = queue.popleft()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return res

日期

2018 年 3 月 14 日 –霍金去世日

【LeetCode】199. Binary Tree Right Side View 解题报告(Python)的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  2. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  3. [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 ...

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

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

  6. [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 ...

  7. (二叉树 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 ...

  8. [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 ...

  9. 【原创】leetCodeOj --- Binary Tree Right Side View 解题报告

    二连水 题目地址: https://leetcode.com/problems/binary-tree-right-side-view/ 题目内容: Given a binary tree, imag ...

随机推荐

  1. mysql 分组统计、排序、取前N条记录解决方案

    需要在mysql中解决记录的分组统计.排序,并抽取前10条记录的功能.现已解决,解决方案如下: 1)表结构 CREATE TABLE `policy_keywords_rel` ( `id` int( ...

  2. EPOLL原理详解(图文并茂)

    文章核心思想是: 要清晰明白EPOLL为什么性能好. 本文会从网卡接收数据的流程讲起,串联起CPU中断.操作系统进程调度等知识:再一步步分析阻塞接收数据.select到epoll的进化过程:最后探究e ...

  3. SonarQube的部分规则探讨

    引言:为了更好的使项目代码规范化,减少Bug的出现,因此最近引入了SonarQube来帮助检测代码问题,这里就分享部分有趣的规则. 注:因为保密原则,文章贴出来的代码都是我按照格式仿写的,并非公司源码 ...

  4. c#图标、显示图表、图形、json echarts实例 数据封装【c#】

    page: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Viewxxx ...

  5. 14 - springboot的@Configuration、@Bean、@Import()、@ImportResource()、@Conditional说明

    1.@Configuration.@Bean.@Import().@ImportResource().@Conditional 分析源码的时候总会见到标题中的这几个注解,因此:弄一篇博客来说明一下吧, ...

  6. javaSE高级篇1 — 异常与多线程基础

    1.异常的体系结构  注:Throwable是一个类,不是一个接口,这个类里面是描述的一些Error和Exception的共性,如图所示: 异常 / 错误是什么意思? 定义:指的是程序运行过程中,可能 ...

  7. Shell 打印空行的行号

    目录 Shell 打印空行的行号 题解 Shell 打印空行的行号 写一个 bash脚本以输出一个文本文件 nowcoder.txt中空行的行号,可能连续,从1开始 示例: 假设 nowcoder.t ...

  8. keybd_event模拟键盘按键,mouse_event怎么用

    从 模仿UP主,用Python实现一个弹幕控制的直播间! - 蛮三刀酱 - 博客园 (cnblogs.com) 知道了 PyAutoGUI: * Moving the mouse and clicki ...

  9. academy

    academy at/in school都行,academy一般用at. The word comes from the Academy in ancient Greece, which derive ...

  10. 【swift】长按事件绑定,平移滑动事件+坐标获取

    为何把这两个事件归类在一起? 我后来才明白,iOS有一个手势事件(UiGestureRecognizer) 事件里有7个功能,不过我只试过前两个,也就是标题的这两个(长按.平移滑动) UILongPr ...