您的位置:首页 > 其它

leetcode-199-二叉树的右视图

2019-07-20 21:03 113 查看
原文链接:http://www.cnblogs.com/oldby/p/11219246.html

题目描述:

第一次提交:

# 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 not root:
return []
stack = [root]
res = []
while stack:
temp = []
res.append(stack[0].val)
for i in stack:
if i.right:
temp.append(i.right)
if i.left:
temp.append(i.left)
stack = temp
return res

方法二:

class Solution(object):
def rightSideView(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
d={}
def f(r,i):
if r:
d[i]=r.val
f(r.left,i+1)
f(r.right,i+1)
f(root,0)
return list(d.values())

 

转载于:https://www.cnblogs.com/oldby/p/11219246.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: