Binary Tree LeetCode Problems: All 174, With Python Solutions
Every problem in this library that LeetCode tags Binary Tree — 174 in total, 128 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 174 problems
- 38 Easy
- 120 Medium
- 16 Hard
How Binary Tree problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Binary Tree problems — each one explains the approach, gives a Python template and states its complexity.
- Tree Traversal — Choose the order — preorder, inorder, postorder, level — and the problem solves itself.
Binary Tree problems by difficulty
Problems with a complete Python solution are listed first, then by ascending problem number.
Easy (38)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 94 | Binary Tree Inorder Traversal | Easy | Stack, Tree, Depth-First Search +1 |
| 100 | Same Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 101 | Symmetric Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 104 | Maximum Depth of Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 108 | Convert Sorted Array to Binary Search Tree | Easy | Tree, Binary Search Tree, Array +2 |
| 110 | Balanced Binary Tree | Easy | Tree, Depth-First Search, Binary Tree |
| 111 | Minimum Depth of Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 112 | Path Sum | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 144 | Binary Tree Preorder Traversal | Easy | Stack, Tree, Depth-First Search +1 |
| 145 | Binary Tree Postorder Traversal | Easy | Stack, Tree, Depth-First Search +1 |
| 222 | Count Complete Tree Nodes | Easy | Bit Manipulation, Tree, Binary Search +1 |
| 226 | Invert Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 257 | Binary Tree Paths | Easy | Tree, Depth-First Search, String +2 |
| 404 | Sum of Left Leaves | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 501 | Find Mode in Binary Search Tree | Easy | Tree, Depth-First Search, Binary Search Tree +1 |
| 530 | Minimum Absolute Difference in BST | Easy | Tree, Depth-First Search, Breadth-First Search +2 |
| 543 | Diameter of Binary Tree | Easy | Tree, Depth-First Search, Binary Tree |
| 563 | Binary Tree Tilt | Easy | Tree, Depth-First Search, Binary Tree |
| 572 | Subtree of Another Tree | Easy | Tree, Depth-First Search, Binary Tree +2 |
| 617 | Merge Two Binary Trees | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 637 | Average of Levels in Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 653 | Two Sum IV - Input is a BST | Easy | Tree, Depth-First Search, Breadth-First Search +4 |
| 671 | Second Minimum Node In a Binary Tree | Easy | Tree, Depth-First Search, Binary Tree |
| 700 | Search in a Binary Search Tree | Easy | Tree, Binary Search Tree, Binary Tree |
| 703 | Kth Largest Element in a Stream | Easy | Tree, Design, Binary Search Tree +3 |
| 783 | Minimum Distance Between BST Nodes | Easy | Tree, Depth-First Search, Breadth-First Search +2 |
| 872 | Leaf-Similar Trees | Easy | Tree, Depth-First Search, Binary Tree |
| 897 | Increasing Order Search Tree | Easy | Stack, Tree, Depth-First Search +2 |
| 938 | Range Sum of BST | Easy | Tree, Depth-First Search, Binary Search Tree +1 |
| 965 | Univalued Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 993 | Cousins in Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 1022 | Sum of Root To Leaf Binary Numbers | Easy | Tree, Depth-First Search, Binary Tree |
| 1379 | Find a Corresponding Node of a Binary Tree in a Clone of That Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 2236 | Root Equals Sum of Children | Easy | Tree, Binary Tree |
| 2331 | Evaluate Boolean Binary Tree | Easy | Tree, Depth-First Search, Binary Tree |
| 270 | Closest Binary Search Tree ValuePremium | Easy | Tree, Depth-First Search, Binary Search Tree +2 |
| 1469 | Find All The Lonely NodesPremium | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 2689 | Extract Kth Character From The Rope TreePremium | Easy | Tree, Depth-First Search, Binary Tree |
Medium (120)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 95 | Unique Binary Search Trees II | Medium | Tree, Binary Search Tree, Dynamic Programming +2 |
| 96 | Unique Binary Search Trees | Medium | Tree, Binary Search Tree, Math +2 |
| 98 | Validate Binary Search Tree | Medium | Tree, Depth-First Search, Binary Search Tree +1 |
| 99 | Recover Binary Search Tree | Medium | Tree, Depth-First Search, Binary Search Tree +1 |
| 102 | Binary Tree Level Order Traversal | Medium | Tree, Breadth-First Search, Binary Tree |
| 103 | Binary Tree Zigzag Level Order Traversal | Medium | Tree, Breadth-First Search, Binary Tree |
| 105 | Construct Binary Tree from Preorder and Inorder Traversal | Medium | Tree, Array, Hash Table +2 |
| 106 | Construct Binary Tree from Inorder and Postorder Traversal | Medium | Tree, Array, Hash Table +2 |
| 107 | Binary Tree Level Order Traversal II | Medium | Tree, Breadth-First Search, Binary Tree |
| 109 | Convert Sorted List to Binary Search Tree | Medium | Tree, Binary Search Tree, Linked List +2 |
| 113 | Path Sum II | Medium | Tree, Depth-First Search, Backtracking +1 |
| 114 | Flatten Binary Tree to Linked List | Medium | Stack, Tree, Depth-First Search +2 |
| 116 | Populating Next Right Pointers in Each Node | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 117 | Populating Next Right Pointers in Each Node II | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 129 | Sum Root to Leaf Numbers | Medium | Tree, Depth-First Search, Binary Tree |
| 173 | Binary Search Tree Iterator | Medium | Stack, Tree, Design +3 |
| 199 | Binary Tree Right Side View | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 230 | Kth Smallest Element in a BST | Medium | Tree, Depth-First Search, Binary Search Tree +1 |
| 235 | Lowest Common Ancestor of a Binary Search Tree | Medium | Tree, Depth-First Search, Binary Search Tree +1 |
| 236 | Lowest Common Ancestor of a Binary Tree | Medium | Tree, Depth-First Search, Binary Tree |
| 331 | Verify Preorder Serialization of a Binary Tree | Medium | Stack, Tree, String +1 |
| 337 | House Robber III | Medium | Tree, Depth-First Search, Dynamic Programming +1 |
| 437 | Path Sum III | Medium | Tree, Depth-First Search, Binary Tree |
| 449 | Serialize and Deserialize BST | Medium | Tree, Depth-First Search, Breadth-First Search +4 |
| 450 | Delete Node in a BST | Medium | Tree, Binary Search Tree, Binary Tree |
| 508 | Most Frequent Subtree Sum | Medium | Tree, Depth-First Search, Hash Table +1 |
| 513 | Find Bottom Left Tree Value | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 515 | Find Largest Value in Each Tree Row | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 538 | Convert BST to Greater Tree | Medium | Tree, Depth-First Search, Binary Search Tree +1 |
| 606 | Construct String from Binary Tree | Medium | Tree, Depth-First Search, String +1 |
| 623 | Add One Row to Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 652 | Find Duplicate Subtrees | Medium | Tree, Depth-First Search, Hash Table +1 |
| 654 | Maximum Binary Tree | Medium | Stack, Tree, Array +3 |
| 655 | Print Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 662 | Maximum Width of Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 669 | Trim a Binary Search Tree | Medium | Tree, Depth-First Search, Binary Search Tree +1 |
| 687 | Longest Univalue Path | Medium | Tree, Depth-First Search, Binary Tree |
| 701 | Insert into a Binary Search Tree | Medium | Tree, Binary Search Tree, Binary Tree |
| 814 | Binary Tree Pruning | Medium | Tree, Depth-First Search, Binary Tree |
| 863 | All Nodes Distance K in Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 865 | Smallest Subtree with all the Deepest Nodes | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 889 | Construct Binary Tree from Preorder and Postorder Traversal | Medium | Tree, Array, Hash Table +2 |
| 894 | All Possible Full Binary Trees | Medium | Tree, Recursion, Memoization +2 |
| 919 | Complete Binary Tree Inserter | Medium | Tree, Breadth-First Search, Design +1 |
| 951 | Flip Equivalent Binary Trees | Medium | Tree, Depth-First Search, Binary Tree |
| 958 | Check Completeness of a Binary Tree | Medium | Tree, Breadth-First Search, Binary Tree |
| 971 | Flip Binary Tree To Match Preorder Traversal | Medium | Tree, Depth-First Search, Binary Tree |
| 979 | Distribute Coins in Binary Tree | Medium | Tree, Depth-First Search, Binary Tree |
| 988 | Smallest String Starting From Leaf | Medium | Tree, Depth-First Search, String +2 |
| 998 | Maximum Binary Tree II | Medium | Tree, Binary Tree |
| 1008 | Construct Binary Search Tree from Preorder Traversal | Medium | Stack, Tree, Binary Search Tree +3 |
| 1026 | Maximum Difference Between Node and Ancestor | Medium | Tree, Depth-First Search, Binary Tree |
| 1038 | Binary Search Tree to Greater Sum Tree | Medium | Tree, Depth-First Search, Binary Search Tree +1 |
| 1080 | Insufficient Nodes in Root to Leaf Paths | Medium | Tree, Depth-First Search, Binary Tree |
| 1104 | Path In Zigzag Labelled Binary Tree | Medium | Tree, Math, Binary Tree |
| 1110 | Delete Nodes And Return Forest | Medium | Tree, Depth-First Search, Array +2 |
| 1123 | Lowest Common Ancestor of Deepest Leaves | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1145 | Binary Tree Coloring Game | Medium | Tree, Depth-First Search, Binary Tree |
| 1161 | Maximum Level Sum of a Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1261 | Find Elements in a Contaminated Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 1302 | Deepest Leaves Sum | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1305 | All Elements in Two Binary Search Trees | Medium | Tree, Depth-First Search, Binary Search Tree +2 |
| 1315 | Sum of Nodes with Even-Valued Grandparent | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1325 | Delete Leaves With a Given Value | Medium | Tree, Depth-First Search, Binary Tree |
| 1339 | Maximum Product of Splitted Binary Tree | Medium | Tree, Depth-First Search, Binary Tree |
| 1361 | Validate Binary Tree Nodes | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 1367 | Linked List in Binary Tree | Medium | Tree, Depth-First Search, Linked List +1 |
| 1372 | Longest ZigZag Path in a Binary Tree | Medium | Tree, Depth-First Search, Dynamic Programming +1 |
| 1382 | Balance a Binary Search Tree | Medium | Greedy, Tree, Depth-First Search +3 |
| 1448 | Count Good Nodes in Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1457 | Pseudo-Palindromic Paths in a Binary Tree | Medium | Bit Manipulation, Tree, Depth-First Search +2 |
| 1530 | Number of Good Leaf Nodes Pairs | Medium | Tree, Depth-First Search, Binary Tree |
| 1609 | Even Odd Tree | Medium | Tree, Breadth-First Search, Binary Tree |
| 2049 | Count Nodes With the Highest Score | Medium | Tree, Depth-First Search, Array +1 |
| 2096 | Step-By-Step Directions From a Binary Tree Node to Another | Medium | Tree, Depth-First Search, String +1 |
| 2196 | Create Binary Tree From Descriptions | Medium | Tree, Array, Hash Table +1 |
| 2265 | Count Nodes Equal to Average of Subtree | Medium | Tree, Depth-First Search, Binary Tree |
| 2385 | Amount of Time for Binary Tree to Be Infected | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 2415 | Reverse Odd Levels of Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 2471 | Minimum Number of Operations to Sort a Binary Tree by Level | Medium | Tree, Breadth-First Search, Binary Tree |
| 2476 | Closest Nodes Queries in a Binary Search Tree | Medium | Tree, Depth-First Search, Binary Search Tree +3 |
| 2583 | Kth Largest Sum in a Binary Tree | Medium | Tree, Breadth-First Search, Binary Tree +1 |
| 2641 | Cousins in Binary Tree II | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 2673 | Make Costs of Paths Equal in a Binary Tree | Medium | Greedy, Tree, Array +2 |
| 156 | Binary Tree Upside DownPremium | Medium | Tree, Depth-First Search, Binary Tree |
| 250 | Count Univalue SubtreesPremium | Medium | Tree, Depth-First Search, Binary Tree |
| 255 | Verify Preorder Sequence in Binary Search TreePremium | Medium | Stack, Tree, Binary Search Tree +4 |
| 285 | Inorder Successor in BSTPremium | Medium | Tree, Depth-First Search, Binary Search Tree +1 |
| 298 | Binary Tree Longest Consecutive SequencePremium | Medium | Tree, Depth-First Search, Binary Tree |
| 314 | Binary Tree Vertical Order TraversalPremium | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 333 | Largest BST SubtreePremium | Medium | Tree, Depth-First Search, Binary Search Tree +2 |
| 366 | Find Leaves of Binary TreePremium | Medium | Tree, Depth-First Search, Binary Tree |
| 426 | Convert Binary Search Tree to Sorted Doubly Linked ListPremium | Medium | Stack, Tree, Depth-First Search +4 |
| 510 | Inorder Successor in BST IIPremium | Medium | Tree, Binary Search Tree, Binary Tree |
| 536 | Construct Binary Tree from StringPremium | Medium | Stack, Tree, Depth-First Search +2 |
| 545 | Boundary of Binary TreePremium | Medium | Tree, Depth-First Search, Binary Tree |
| 549 | Binary Tree Longest Consecutive Sequence IIPremium | Medium | Tree, Depth-First Search, Binary Tree |
| 663 | Equal Tree PartitionPremium | Medium | Tree, Depth-First Search, Binary Tree |
| 666 | Path Sum IVPremium | Medium | Tree, Depth-First Search, Array +2 |
| 742 | Closest Leaf in a Binary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 776 | Split BSTPremium | Medium | Tree, Binary Search Tree, Recursion +1 |
| 1120 | Maximum Average SubtreePremium | Medium | Tree, Depth-First Search, Binary Tree |
| 1214 | Two Sum BSTsPremium | Medium | Stack, Tree, Depth-First Search +4 |
| 1430 | Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1485 | Clone Binary Tree With Random PointerPremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1586 | Binary Search Tree Iterator IIPremium | Medium | Stack, Tree, Design +3 |
| 1602 | Find Nearest Right Node in Binary TreePremium | Medium | Tree, Breadth-First Search, Binary Tree |
| 1612 | Check If Two Expression Trees are EquivalentPremium | Medium | Tree, Depth-First Search, Hash Table +2 |
| 1628 | Design an Expression Tree With Evaluate FunctionPremium | Medium | Stack, Tree, Design +3 |
| 1644 | Lowest Common Ancestor of a Binary Tree IIPremium | Medium | Tree, Depth-First Search, Binary Tree |
| 1650 | Lowest Common Ancestor of a Binary Tree IIIPremium | Medium | Tree, Hash Table, Two Pointers +1 |
| 1660 | Correct a Binary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1666 | Change the Root of a Binary TreePremium | Medium | Tree, Depth-First Search, Binary Tree |
| 1676 | Lowest Common Ancestor of a Binary Tree IVPremium | Medium | Tree, Depth-First Search, Hash Table +1 |
| 1740 | Find Distance in a Binary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1902 | Depth of BST Given Insertion OrderPremium | Medium | Tree, Binary Search Tree, Array +2 |
| 1973 | Count Nodes Equal to Sum of DescendantsPremium | Medium | Tree, Depth-First Search, Binary Tree |
| 2445 | Number of Nodes With Value OnePremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 2764 | Is Array a Preorder of Some Binary TreePremium | Medium | Stack, Tree, Depth-First Search +1 |
| 2773 | Height of Special Binary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
Hard (16)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 124 | Binary Tree Maximum Path Sum | Hard | Tree, Depth-First Search, Dynamic Programming +1 |
| 297 | Serialize and Deserialize Binary Tree | Hard | Tree, Depth-First Search, Breadth-First Search +3 |
| 968 | Binary Tree Cameras | Hard | Tree, Depth-First Search, Dynamic Programming +1 |
| 987 | Vertical Order Traversal of a Binary Tree | Hard | Tree, Depth-First Search, Breadth-First Search +3 |
| 1028 | Recover a Tree From Preorder Traversal | Hard | Tree, Depth-First Search, String +1 |
| 1373 | Maximum Sum BST in Binary Tree | Hard | Tree, Depth-First Search, Binary Search Tree +2 |
| 1569 | Number of Ways to Reorder Array to Get Same BST | Hard | Tree, Union Find, Binary Search Tree +7 |
| 2458 | Height of Binary Tree After Subtree Removal Queries | Hard | Tree, Depth-First Search, Breadth-First Search +2 |
| 2509 | Cycle Length Queries in a Tree | Hard | Tree, Array, Binary Tree |
| 272 | Closest Binary Search Tree Value IIPremium | Hard | Stack, Tree, Depth-First Search +4 |
| 431 | Encode N-ary Tree to Binary TreePremium | Hard | Tree, Depth-First Search, Breadth-First Search +2 |
| 1597 | Build Binary Expression Tree From Infix ExpressionPremium | Hard | Stack, Tree, String +1 |
| 1932 | Merge BSTs to Create Single BST | Hard | Tree, Depth-First Search, Hash Table +2 |
| 2005 | Subtree Removal Game with Fibonacci TreePremium | Hard | Tree, Math, Dynamic Programming +2 |
| 2313 | Minimum Flips in Binary Tree to Get ResultPremium | Hard | Tree, Depth-First Search, Dynamic Programming +1 |
| 2792 | Count Nodes That Are Great EnoughPremium | Hard | Tree, Depth-First Search, Divide and Conquer +1 |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Sorting392
- Greedy346
- Depth-First Search289
- Binary Search253
- Database249
- Tree225
- Breadth-First Search223
- Matrix216
- Two Pointers201
- Bit Manipulation194
- Heap (Priority Queue)163
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Counting126
- Design122
- Sliding Window116
- Backtracking105
When the Binary Tree problem arrives live
Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.