pyPython Practice LabFrom first print to final round
TOPIC 15 OF 18

Trees and Tries Python lessons

Traversal, recursion on hierarchies, binary-search-tree rules, prefix lookup, and subtree reasoning.

13 exercises8+ conceptsBeginner → interview context
Start this topic
WHAT YOU WILL LEARN

Core concepts

Traverse a binary tree by levelsFind binary-tree maximum depthValidate a binary search treeFind lowest common ancestorFind kth smallest value in a BSTSerialize and deserialize a binary treeBuild a tree from preorder and inorderView a binary tree from the right
REUSABLE THINKING

Problem-solving patterns

  • tree · BFS
  • tree · depth
  • BST · bounds
  • binary tree · recursion · LeetCode pattern
  • BST · inorder · company screen
LESSON OUTCOME

What this topic builds

Learn the idea once, trace it through two verified cases, then explain why your approach works.

ORDERED LESSONS

All 13 lessons in this topic

Work from top to bottom to build each concept gradually.

  1. #137
    Traverse a binary tree by levels

    Print each depth of a small complete tree on its own line.

    BeginnerPremium
  2. #138
    Find binary-tree maximum depth

    Find the depth of a tree whose longest path contains three nodes.

    IntermediatePremium
  3. #139
    Validate a binary search tree

    Confirm that all nodes obey the global binary-search-tree ordering rule.

    IntermediatePremium
  4. #140
    Find lowest common ancestor

    Find the lowest node whose subtree contains both requested values.

    IntermediatePremium
  5. #141
    Find kth smallest value in a BST

    Return the third value in the BST's increasing order.

    AdvancedPremium
  6. #142
    Serialize and deserialize a binary tree

    Serialize a sparse tree, deserialize it, and show the preserved tokens.

    AdvancedPremium
  7. #143
    Build a tree from preorder and inorder

    Reconstruct a tree and print its level-order values.

    AdvancedPremium
  8. #144
    View a binary tree from the right

    Return the node visible from the right at every tree level.

    Interview-LevelPremium
  9. #145
    Calculate binary-tree diameter

    Find the longest edge-count path between any two nodes.

    Interview-LevelPremium
  10. #146
    List root-to-leaf target paths

    List every root-to-leaf path whose values reach the target.

    Interview-LevelPremium
  11. #147
    Implement a prefix trie

    Insert words, then distinguish exact matches from valid prefixes.

    Interview-LevelPremium
  12. #148
    Find dictionary words in a board

    Find all dictionary words that can be formed by adjacent cells without reuse.

    Interview-LevelPremium
  13. #149
    Find maximum binary-tree path sum

    Find the greatest sum along any connected path in the tree.

    Interview-LevelPremium