# 366. Find Leaves of Binary Tree

{% embed url="<https://leetcode.com/problems/find-leaves-of-binary-tree/>" %}

Level traversal 是把层级从上往下传递，这个按高度记录节点 `height traversal`（或者说计算节点到叶子节点的距离）是把高度从下往上传递，iterative就比较麻烦，借助recursive会比较方便地写出来。

叶子节点高度为1，往上每个节点的高度都取左右子树较大值+1，一边递归一边记录。

```python
class Solution(object):
    def findLeaves(self, root):
        def postorder(root, res):
            if not root: return 0
            height = max(postorder(root.left, res), postorder(root.right, res))+1
            res[height].append(root.val)
            return height
        res = collections.defaultdict(list)
        postorder(root, res)
        return res.values()
```
