---
# System prepended metadata

title: SE103 Binary Trees I Breakout Session Solutions

---

# SE103 Binary Trees I Breakout Session Solutions
## [Reverse Level Order Traversal]()
```
from collections import deque

def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
    if root == None:
        return []
    queue = deque()
    queue.append([root])
    stack = deque()
    while len(queue) > 0:
        nodesInLevel = queue.popleft()
        resultForLevel = []
        nextLevel = []
        for node in nodesInLevel:
            resultForLevel.append(node.val)
            if node.left != None:
                nextLevel.append(node.left)
            if node.right != None:
                nextLevel.append(node.right)
        if len(nextLevel) > 0:
            queue.append(nextLevel)
        stack.append(resultForLevel)
    res = []
    while len(stack) > 0:
        res.append(stack.pop())
    return res
```
## [Min Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)
```
def minDepth(self, root):
    """
    :type root: TreeNode
    :rtype: int
    """
    if root == None:
        return 0
    queue = deque()
    queue.append((root, 1))
    while len(queue) > 0:
        curr = queue.popleft()
        currNode, currLevel = curr[0], curr[1]
        if currNode.left == None and currNode.right == None:
            return currLevel
        if currNode.left != None:
            queue.append((currNode.left, currLevel + 1))
        if currNode.right != None:
            queue.append((currNode.right, currLevel + 1))
    return -1
```
## [Binary Tree Pruning](https://leetcode.com/problems/binary-tree-pruning/description/)
```
def pruneTree(self, root: TreeNode) -> TreeNode:
    shouldPruneRoot = self.pruneTreeHelper(root)
    if shouldPruneRoot:
        return None
    return root

def pruneTreeHelper(self, root):
    shouldPruneLeft = shouldPruneRight = True

    if root.left != None:
        shouldPruneLeft = self.pruneTreeHelper(root.left)
        if shouldPruneLeft:
            root.left = None

    if root.right != None:
        shouldPruneRight = self.pruneTreeHelper(root.right)
        if shouldPruneRight:
            root.right = None

    if root.val == 0 and shouldPruneLeft and shouldPruneRight:
        return True
```