Trees and BST in Python
Tree node
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = rightProblems to practice
- Inorder traversal
- Preorder traversal
- Postorder traversal
- Level order traversal
- Maximum depth
- Diameter of tree
- Lowest common ancestor
- Validate BST
- Path sum
Example: inorder traversal
def inorder(root):
if not root:
return []
return inorder(root.left) + [root.val] + inorder(root.right)Can you explain it without the notes?
Trees and BST in Python
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Tree node
Turn this concept into a clear answer.
Practice this conceptAdapted from 18_DSA_PYTHON.md · “7. Trees and BST in Python”. Examples are study fragments; framework behavior can depend on your version.