**Find Leaves of Binary Tree** https://leetcode.com/problems/find-leaves-of-binary-tree/?tab=Description

**Algorithm**

- Use depth first search to collect the leaves.
- Visit should test if root is leaf and should collect it.
- Caller of the method should mutate the tree if called node were a leaf.

```
class Solution(object):
def collect(self, root, row):
if root == None:
return None
elif root.right == None and root.left == None:
row.append(root.val)
return None
else:
root.left = self.collect(root.left, row)
root.right = self.collect(root.right, row)
return root
def findLeaves(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
result = []
while root:
row = []
root = self.collect(root, row)
result.append(row)
return result
```

]]>**Find Leaves of Binary Tree** https://leetcode.com/problems/find-leaves-of-binary-tree/?tab=Description

**Algorithm**

- Use depth first search to collect the leaves.
- Visit should test if root is leaf and should collect it.
- Caller of the method should mutate the tree if called node were a leaf.

```
class Solution(object):
def collect(self, root, row):
if root == None:
return None
elif root.right == None and root.left == None:
row.append(root.val)
return None
else:
root.left = self.collect(root.left, row)
root.right = self.collect(root.right, row)
return root
def findLeaves(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
result = []
while root:
row = []
root = self.collect(root, row)
result.append(row)
return result
```

]]>