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

Trees and Tries Python practice problems

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

26 exercises8+ conceptsBeginner → interview context
Start this topic
WHAT YOU WILL PRACTISE

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
PRACTICE OUTCOME

What this topic builds

Learn the idea once, use it across the exercises, and explain why your approach works.

ORDERED PRACTICE

All 26 problems in this topic

Work from top to bottom to build the concept gradually.

  1. #273
    Traverse a binary tree by levels: Three-level tree

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

    BeginnerPremium
  2. #274
    Traverse a binary tree by levels: Single-node tree

    A one-node tree produces exactly one level.

    BeginnerPremium
  3. #275
    Find binary-tree maximum depth: Unbalanced tree

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

    IntermediatePremium
  4. #276
    Find binary-tree maximum depth: Empty tree

    An empty tree represented by null has depth zero.

    IntermediatePremium
  5. #277
    Validate a binary search tree: Valid BST

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

    IntermediatePremium
  6. #278
    Validate a binary search tree: Hidden violation

    Detect a value that is locally plausible but violates an ancestor's bound.

    IntermediatePremium
  7. #279
    Find lowest common ancestor: Targets in different subtrees

    Find the lowest node whose subtree contains both requested values.

    IntermediatePremium
  8. #280
    Find lowest common ancestor: One node is an ancestor

    Return the higher target when it already contains the other target.

    AdvancedPremium
  9. #281
    Find kth smallest value in a BST: Third smallest

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

    AdvancedPremium
  10. #282
    Find kth smallest value in a BST: Smallest value

    When k is one, return the minimum node.

    AdvancedPremium
  11. #283
    Serialize and deserialize a binary tree: Sparse tree round trip

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

    AdvancedPremium
  12. #284
    Serialize and deserialize a binary tree: Trim redundant trailing nulls

    Remove trailing null markers that do not add structural information.

    AdvancedPremium
  13. #285
    Build a tree from preorder and inorder: Balanced reconstruction

    Reconstruct a tree and print its level-order values.

    AdvancedPremium
  14. #286
    Build a tree from preorder and inorder: Right-skewed reconstruction

    Build a tree whose nodes all lie on the right.

    AdvancedPremium
  15. #287
    View a binary tree from the right: Sparse right view

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

    Interview-LevelPremium
  16. #288
    View a binary tree from the right: Left-only tree

    A left child is visible when no node exists farther right on its level.

    Interview-LevelPremium
  17. #289
    Calculate binary-tree diameter: Diameter crosses root

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

    Interview-LevelPremium
  18. #290
    Calculate binary-tree diameter: Skewed tree

    The diameter of a one-sided chain is its number of edges.

    Interview-LevelPremium
  19. #291
    List root-to-leaf target paths: Two valid paths

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

    Interview-LevelPremium
  20. #292
    List root-to-leaf target paths: No target path

    Report None when no leaf completes the target sum.

    Interview-LevelPremium
  21. #293
    Implement a prefix trie: Word and prefix checks

    Insert words, then distinguish exact matches from valid prefixes.

    Interview-LevelPremium
  22. #294
    Implement a prefix trie: Missing prefix

    Return False when no inserted word begins with the requested prefix.

    Interview-LevelPremium
  23. #295
    Find dictionary words in a board: Several board words

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

    Interview-LevelPremium
  24. #296
    Find dictionary words in a board: No word can be formed

    Report None when every dictionary word is impossible on the board.

    Interview-LevelPremium
  25. #297
    Find maximum binary-tree path sum: Best path crosses a subtree root

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

    Interview-LevelPremium
  26. #298
    Find maximum binary-tree path sum: All negative values

    Choose the least negative single node when every extension would reduce the sum.

    Interview-LevelPremium