233-5 Help with B-tree greater_than functions

Hi, need more help on the B-tree mission (https://app.dataquest.io/m/233/performance-boosts-of-using-a-b-tree-ii/5/implementing-the-range-query). I am having trouble understanding the logic behind the answer for the greater_than function. I added some comments to indicate what makes sense and what doesn’t for me. Hope someone has time to explain this. Thanks in advance!

def greater_than(self, node, term, upper_bound=None, inclusive=False):
        # if the tree has no root, return an empty list

        if not self.root:
            return []
        
        # if the root exists, loop through its keys
        # if key < term, increment the index and go to the next key
        # append matched values to the return list
        
        index = 0
        values = []
        for key in node.keys:
            if upper_bound is not None:
                if inclusive and key == upper_bound:
                    values.append(key)
                if key >= upper_bound:
                    break
            if term > key:
                index += 1
                continue
            if inclusive and key == term:
                values.append(key)
            if term < key:
                values.append(key)
    
            # if the node is an internal node, use the recursive function to 
            # continue find matching values on its children's nodes.   
 
            if not node.is_leaf():
                values += self.greater_than(
                    node.children[index],
                    term,
                    upper_bound,
                    inclusive
                )
            index += 1

        # not sure why checking whether the node is not a leaf here.  
        if not node.is_leaf():
            values += self.greater_than(
                node.children[index],
                term,
                upper_bound,
                inclusive
            )
        return values