Skip to content
The Chat Archive
deepseek-v3-0324· DeepSeek LMArena Human Preference 140K

Write a Python program that prints the value of every tree node value of a binary tree.

  1. User

    Write a Python program that prints the value of every tree node value of a binary tree.

  2. Assistant

    Here’s a Python program that prints the value of every node in a binary tree using an in-order traversal. The program includes a simple TreeNode class to represent the nodes of the binary tree and a function to traverse and print all node values.

    Python Program:

    class TreeNode:
        def __init__(self, value):
            self.value = value
            self.left = None
            self.right = None
    
    def print_tree_inorder(node):
        """Prints all node values of a binary tree using in-order traversal."""
        if node is not None:
            print_tree_inorder(node.left)   # Traverse left subtree
            print(node.value, end=" ")      # Print current node
            print_tree_inorder(node.right)  # Traverse right subtree
    
    # Example usage:
    if __name__ == "__main__":
        # Construct a sample binary tree:
        #        1
        #       / \
        #      2   3
        #     / \
        #    4   5
        root = TreeNode(1)
        root.left = TreeNode(2)
        root.right = TreeNode(3)
        root.left.left = TreeNode(4)
        root.left.right = TreeNode(5)
    
        print("In-order traversal (prints all node values):")
        print_tree_inorder(root)  # Output: 4 2 5 1 3
    

    Explanation:

    1. TreeNode Class:

      • Represents a node in the binary tree with value, left, and right attributes.
    2. print_tree_inorder(node):

      • Recursively traverses the tree in in-order (Left → Root → Right) and prints each node's value.
      • Base Case: If node is None, the function returns without doing anything.
      • Recursive Steps:
        • Traverse the left subtree (print_tree_inorder(node.left)).
        • Print the current node's value (print(node.value, end=" ")).
        • Traverse the right subtree (print_tree_inorder(node.right)).
    3. Example Tree Construction:

      • A sample tree is built for demonstration:
            1
           / \
          2   3
         / \
        4   5
        
      • The in-order traversal prints: 4 2 5 1 3.

    Alternative Traversals:

    If you want to print nodes in pre-order (Root → Left → Right) or post-order (Left → Right → Root), you can modify the function as follows:

    Pre-order Traversal:

    def print_tree_preorder(node):
        if node is not None:
            print(node.value, end=" ")      # Print current node first
            print_tree_preorder(node.left)  # Then left subtree
            print_tree_preorder(node.right) # Then right subtree
    

    Output: 1 2 4 5 3

    Post-order Traversal:

    def print_tree_postorder(node):
        if node is not None:
            print_tree_postorder(node.left)  # Traverse left subtree first
            print_tree_postorder(node.right) # Then right subtree
            print(node.value, end=" ")       # Print current node last
    

    Output: 4 5 2 3 1

    Conclusion:

    This program efficiently prints all node values in a binary tree using in-order traversal. You can easily modify it for pre-order or post-order traversal by changing the order of the recursive calls.

    Would you like any modifications or additional features (e.g., level-order traversal using BFS)? 😊

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.