题解 | #输出二叉树的右视图#
输出二叉树的右视图
https://www.nowcoder.com/practice/c9480213597e45f4807880c763ddd5f0
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 求二叉树的右视图 # @param preOrder int整型一维数组 先序遍历 # @param inOrder int整型一维数组 中序遍历 # @return int整型一维数组 # from collections import defaultdict class Solution: def __init__(self): self.level = list() def solve(self, xianxu, zhongxu): # write code here if len(xianxu) == 0: return [] if len(xianxu) == 1: return xianxu self.reverse(xianxu, zhongxu, 0) return self.level def reverse(self, xianxu, zhongxu, level): if len(xianxu) == 0: return root = xianxu[0] index = zhongxu.index(root) if len(self.level) == level: self.level.append(root) self.reverse(xianxu[index + 1 :], zhongxu[index + 1 :], level + 1) self.reverse(xianxu[1 : index + 1], zhongxu[:index], level + 1)