5 Best Ways to Find Leaf and Non-Leaf Nodes of a Binary Tree in Python

Rate this post

πŸ’‘ Problem Formulation: Binary trees play a critical role in computer science, and identifying their leaf and non-leaf nodes is a common task for many algorithms. A leaf node is a node with no children, while a non-leaf (internal) node is one with at least one child. Given a binary tree, the goal is to find and distinguish these two types of nodes. For instance, in a binary tree with elements [1, 2, 3, 4, 5], nodes 4 and 5 would be leaf nodes, while nodes 1, 2, and 3 would be non-leaf nodes.

Method 1: Recursive Traversal

The recursive traversal method involves a function that traverses the binary tree starting from the root. If a node has no children, it’s a leaf; otherwise, it’s a non-leaf node. The function is called recursively for left and right children.

Here’s an example:

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

def find_leaf_and_non_leaf(root, leaves, non_leaves):
    if root:
        if root.left is None and root.right is None:
            leaves.append(root.value)
        else:
            non_leaves.append(root.value)
        find_leaf_and_non_leaf(root.left, leaves, non_leaves)
        find_leaf_and_non_leaf(root.right, leaves, non_leaves)

# Example usage:
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)

leaves = []
non_leaves = []
find_leaf_and_non_leaf(root, leaves, non_leaves)
print("Leaves:", leaves)
print("Non-leaves:", non_leaves)

The output of this code snippet:

Leaves: [4, 5] Non-leaves: [1, 2, 3]

This simple recursive algorithm efficiently traverses the tree to find all the leaf and non-leaf nodes. By checking if both the left and right children of a node are absent, it successfully identifies the leaf nodes and gathers them in one list, while the non-leaf nodes are collected in another.

Method 2: Iterative Depth-First Search

The iterative Depth-First Search (DFS) approach uses a stack data structure to mimic the recursive stack and traverse the tree depth-first. It employs a similar logic to check for leaf and non-leaf nodes but does so in an iterative manner.

Here’s an example:

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

def find_leaf_and_non_leaf_iteratively(root):
    if not root:
        return [], []
    stack, leaves, non_leaves = [root], [], []
    while stack:
        node = stack.pop()
        if node.left or node.right:
            non_leaves.append(node.value)
            if node.right:
                stack.append(node.right)
            if node.left:
                stack.append(node.left)
        else:
            leaves.append(node.value)
    return leaves, non_leaves

# Example usage:
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)

leaves, non_leaves = find_leaf_and_non_leaf_iteratively(root)
print("Leaves:", leaves)
print("Non-leaves:", non_leaves)

The output of this code snippet:

Leaves: [4, 5] Non-leaves: [1, 2, 3]

The iterative DFS method effectively traverses the binary tree without the overhead of recursive function calls. A stack is used to keep track of the nodes to visit. The current node is examined, and then the children are added to the stack for subsequent visits.

Method 3: Level Order Traversal

Level order traversal is performed using a queue to visit nodes level by level. While traversing, leaf nodes are identified as those without children. All others are non-leaf nodes. This approach provides results in top-down order from the root.

Here’s an example:

from collections import deque

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

def find_leaf_and_non_leaf_level_order(root):
    if not root:
        return [], []
    queue, leaves, non_leaves = deque([root]), [], []
    while queue:
        node = queue.popleft()
        if node.left or node.right:
            non_leaves.append(node.value)
            if node.left:
                queue.append(node.left)
            if node.right:
                queue.append(node.right)
        else:
            leaves.append(node.value)
    return leaves, non_leaves

# Example usage:
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)

leaves, non_leaves = find_leaf_and_non_leaf_level_order(root)
print("Leaves:", leaves)
print("Non-leaves:", non_leaves)

The output of this code snippet:

Leaves: [4, 5] Non-leaves: [1, 2, 3]

This particular manner of traversing a tree guarantees that you encounter nodes level by level, which could be beneficial for certain applications that require such ordering. The deque data structure allows for efficient addition and removal of nodes during the traversal.

Method 4: Morris Traversal

Morris Traversal is a way to traverse the binary tree without using extra space for stack or recursion. It relies on the property that predecessor nodes have a null right child, which can be temporarily modified to establish a link back to the current node. This method is complex but efficient in terms of space usage.

Here’s an example:

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

def find_leaf_and_non_leaf_morris(root):
    current = root
    leaves, non_leaves = [], []
    while current is not None:
        if current.left is None:
            if current.right is None:
                leaves.append(current.value)
            else:
                non_leaves.append(current.value)
            current = current.right
        else:
            pre = current.left
            while pre.right is not None and pre.right != current:
                pre = pre.right
            if pre.right is None:
                non_leaves.append(current.value)
                pre.right = current
                current = current.left
            else:
                pre.right = None
                current = current.right
    return leaves, non_leaves

# Example usage:
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)

leaves, non_leaves = find_leaf_and_non_leaf_morris(root)
print("Leaves:", leaves)
print("Non-leaves:", non_leaves)

The output of this code snippet:

Leaves: [4, 5] Non-leaves: [1, 2, 3]

Morris Traversal is a space-efficient approach that manipulates the tree by establishing temporary links back to the parent node. This allows it to traverse the tree without additional memory for a stack or queue.

Bonus One-Liner Method 5: Using Generators

Python generators can be used to yield leaf and non-leaf nodes in a single line of code by combining conditional expressions and recursion directly inside a generator expression.

Here’s an example:

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

def leaf_and_non_leaf_nodes(node):
    return [*leaf_and_non_leaf_nodes(node.left), node.value, *leaf_and_non_leaf_nodes(node.right)] if node else []

# Example usage:
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)

nodes = leaf_and_non_leaf_nodes(root)
leaves = [node for node in nodes if nodes.count(node) == 1]
non_leaves = list(set(nodes) - set(leaves))
print("Leaves:", leaves)
print("Non-leaves:", non_leaves)

The output of this code snippet:

Leaves: [4, 5] Non-leaves: [1, 2, 3]

This generator-based method compresses the traversal logic into a single line, elegantly yielding the results. This form of expression is concise but may not be the most efficient regarding memory usage and readability.

Summary/Discussion

  • Method 1: Recursive Traversal. Simple and elegant. It can lead to a stack overflow with very deep trees.
  • Method 2: Iterative DFS. Avoids recursive stack limits. Iterative logic might be less intuitive.
  • Method 3: Level Order Traversal. Node levels are preserved. Requires extra memory for the queue.
  • Method 4: Morris Traversal. Space efficient. Alters the tree structure temporarily and is complex to understand.
  • Bonus Method 5: Using Generators. A one-liner approach that is elegant, but not memory efficient and less readable for complex trees.