【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
【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)的更多相关文章
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [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 ...
- 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 (DFS/BFS)
https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...
- [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 ...
- (二叉树 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 ...
- [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 ...
- 【原创】leetCodeOj --- Binary Tree Right Side View 解题报告
二连水 题目地址: https://leetcode.com/problems/binary-tree-right-side-view/ 题目内容: Given a binary tree, imag ...
随机推荐
- SIG -MESH -1
协议栈 node:成为蓝牙mesh网络中一员的设备被称为节点(Node). 蓝牙mesh规格定义了节点可能拥有的特性.具有这些特性中的一个或多个,即表示节点可以在网络中扮演相应的特殊角色.定义的 ...
- JavaScript获取html表单值验证后跳转网页中的关键点
关键代码: 1.表单部分 <form action="Depart.jsp" name="myform" method="post" ...
- 巩固javaweb第十六天
巩固内容: 下拉框 在注册功能中,地区的选择使用了下拉框,可以从地区选项中选择一个地区.在这个 例子中,只允许选择一个,而在有些情况下,下拉框可以进行多选.所以,从功能上来说, 下拉框具有单选按钮和复 ...
- Redis(一)【基础入门】
目录 一.大型网站的系统特点 二.大型网站架构发展历程 三.从NoSQL说起 四.Redis简介 五.Redis安装 1.上传并解压 2.安装C语言编译环境 3.修改安装位置 4.编译安装 5.启动R ...
- 练习1--爬取btc论坛的title和相应的url
爬不到此论坛的html源码,应该涉及到反爬技术,以后再来解决,代码如下 import requests from lxml import etree import json class BtcSpid ...
- 爬虫系列:使用 MySQL 存储数据
上一篇文章我们讲解了爬虫如何存储 CSV 文件,这篇文章,我们讲解如何将采集到的数据保存到 MySQL 数据库中. MySQL 是目前最受欢迎的开源关系型数据库管理系统.一个开源项目具有如此之竞争力实 ...
- Zookeeper的选举算法和脑裂问题
ZK介绍 ZK = zookeeper ZK是微服务解决方案中拥有服务注册发现最为核心的环境,是微服务的基石.作为服务注册发现模块,并不是只有ZK一种产品,目前得到行业认可的还有:Eureka.Con ...
- linux 彻底卸载mysql
1. 停止 mysql 服务: systemctl stop mysqld.service 2. yum remove mysql (因为 之前是通过 yum -y install 方式安装的 ) ...
- linux系统下命令的学习
本博客是本人工作时做的笔记 ps aux |grep ^profile |grep A190200024 ^ 表示行首匹配 linux查看文件大小: 具体可查看:https://www.cnblogs ...
- 设计风格之REST
一.简介 REST简介 REST 是英文 representational state transfer(表象性状态转变)或者表述性状态转 移;Rest 是 web 服务的一种架构风格;使用 HTTP ...