199. Binary Tree Right Side View
该题的本质还是层次遍历,只不过仅把每层的最右边元素记录下来
solution:
class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
if not root:
return []
que = [root]
que1 = []
res = []
while que:
res.append(que[-1].val) # 记录每层最右边的元素
for tmp in que:
if tmp.left:
que1.append(tmp.left)
if tmp.right:
que1.append(tmp.right)
que = que1[:]
que1.clear()
return res