Leetcode199. 二叉树的右视图——python求解

199. 二叉树的右视图

给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:

输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-right-side-view
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

使用DFS算法可得

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def rightSideView(self, root: TreeNode) -> List[int]:
        rightside = dict()
        stack = [(root,0)]
        maxdepth = -1
        while stack:
            node,depth = stack.pop()
            if node is not None:
                maxdepth = max(maxdepth,depth)
                rightside.setdefault(depth,node.val)
                stack.append((node.left,depth+1))
                stack.append((node.right,depth+1))
        return [rightside[i] for i in range(maxdepth+1)]

猜你喜欢

转载自blog.csdn.net/weixin_41729258/article/details/105695858