一個二叉樹,從右邊看,輸出你看到的從上到下的順序python
其實就是遍歷每一層app
#!/usr/bin/env python # -*- coding:utf-8 -*- # Power by WenBin 2017-02-23 20:49:59 For example: Given the following binary tree, 1 <--- / \ 2 3 <--- \ \ 5 4 <--- You should return [1, 3, 4]. class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution(object): def findright(self, root, level, re): if root is not None: if len(re) == level: re.append(root.val) self.levelorder(root.right, level+1, re) self.levelorder(root.left, level+1, re) def rightSideView(self, root): """ :type root: TreeNode :rtype: List[int] """ re = [] self.findright(root, 0, re) return re