首页 > 试题广场 >

算法题:从右边看被遮挡的二叉树,求露出的node

[问答题]

算法题:从右边看被遮挡的二叉树,求露出的node

# 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):
        d = {}
        def f(root, i):   # i为树的深度
            if root:
                d[i] = root.val
                f(root.left, i+1)
                f(root.right, i+1)
        f(root, 0)
        return list(d.values())



从右边往左看二叉树   也就是层次遍历 取每一层最末的那个节点
发表于 2019-07-18 23:04:22 回复(0)
这“露出”用的就离谱。。。

原题出自LeetCode:

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.
大概意思就是给定一棵二叉树,想象自己站在树的右边,返回从上到下你能看到的节点的值。
发表于 2020-07-09 17:10:04 回复(0)
这个题目是什么意思???
发表于 2019-05-30 21:39:35 回复(0)