Tree LeetCode Problems: All 225, With Python Solutions
Every problem in this library that LeetCode tags Tree — 225 in total, 167 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 225 problems
- 41 Easy
- 142 Medium
- 42 Hard
How Tree problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve 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.
Tree problems by difficulty
Showing the first 200 of 225 problems. Problems with a complete Python solution are listed first, then by ascending problem number.
Easy (40)
| # | 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 |
| 559 | Maximum Depth of N-ary Tree | Easy | Tree, Depth-First Search, Breadth-First Search |
| 563 | Binary Tree Tilt | Easy | Tree, Depth-First Search, Binary Tree |
| 572 | Subtree of Another Tree | Easy | Tree, Depth-First Search, Binary Tree +2 |
| 589 | N-ary Tree Preorder Traversal | Easy | Stack, Tree, Depth-First Search |
| 590 | N-ary Tree Postorder Traversal | Easy | Stack, Tree, Depth-First Search |
| 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 |
Medium (125)
| # | 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 |
| 341 | Flatten Nested List Iterator | Medium | Stack, Tree, Depth-First Search +3 |
| 427 | Construct Quad Tree | Medium | Tree, Array, Divide and Conquer +1 |
| 429 | N-ary Tree Level Order Traversal | Medium | Tree, Breadth-First Search |
| 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 |
| 558 | Logical OR of Two Binary Grids Represented as Quad-Trees | Medium | Tree, Divide and Conquer |
| 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 |
| 690 | Employee Importance | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 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 |
| 1376 | Time Needed to Inform All Employees | Medium | Tree, Depth-First Search, Breadth-First Search |
| 1382 | Balance a Binary Search Tree | Medium | Greedy, Tree, Depth-First Search +3 |
| 1443 | Minimum Time to Collect All Apples in a Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 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 |
| 1519 | Number of Nodes in the Sub-Tree With the Same Label | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1530 | Number of Good Leaf Nodes Pairs | Medium | Tree, Depth-First Search, Binary Tree |
| 1600 | Throne Inheritance | Medium | Tree, Depth-First Search, Design +1 |
| 1609 | Even Odd Tree | Medium | Tree, Breadth-First Search, Binary Tree |
| 1993 | Operations on Tree | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 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 |
| 2368 | Reachable Nodes With Restrictions | Medium | Tree, Depth-First Search, Breadth-First Search +4 |
| 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 |
| 2467 | Most Profitable Path in a Tree | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 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 |
| 2477 | Minimum Fuel Cost to Report to the Capital | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 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 |
| 2925 | Maximum Score After Applying Operations on a Tree | Medium | Tree, Depth-First Search, Dynamic Programming |
| 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 |
| 582 | Kill ProcessPremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 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 |
| 1245 | Tree DiameterPremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1257 | Smallest Common RegionPremium | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 1273 | Delete Tree NodesPremium | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 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 |
| 1490 | Clone N-ary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1506 | Find Root of N-Ary TreePremium | Medium | Bit Manipulation, Tree, Depth-First Search +1 |
Hard (35)
| # | 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 |
| 834 | Sum of Distances in Tree | Hard | Tree, Depth-First Search, Graph +1 |
| 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 |
| 1377 | Frog Position After T Seconds | Hard | Tree, Depth-First Search, Breadth-First Search +1 |
| 1483 | Kth Ancestor of a Tree Node | Hard | Bit Manipulation, Tree, Depth-First Search +4 |
| 1569 | Number of Ways to Reorder Array to Get Same BST | Hard | Tree, Union Find, Binary Search Tree +7 |
| 1617 | Count Subtrees With Max Distance Between Cities | Hard | Bit Manipulation, Tree, Dynamic Programming +2 |
| 1719 | Number Of Ways To Reconstruct A Tree | Hard | Tree, Graph |
| 1766 | Tree of Coprimes | Hard | Tree, Depth-First Search, Array +2 |
| 1916 | Count Ways to Build Rooms in an Ant Colony | Hard | Tree, Graph, Topological Sort +3 |
| 2003 | Smallest Missing Genetic Value in Each Subtree | Hard | Tree, Depth-First Search, Union Find +1 |
| 2246 | Longest Path With Different Adjacent Characters | Hard | Tree, Depth-First Search, Graph +3 |
| 2322 | Minimum Score After Removals on a Tree | Hard | Bit Manipulation, Tree, Depth-First Search +1 |
| 2421 | Number of Good Paths | Hard | Tree, Union Find, Graph +3 |
| 2440 | Create Components With Same Value | Hard | Tree, Depth-First Search, Array +2 |
| 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 |
| 2538 | Difference Between Maximum and Minimum Price Sum | Hard | Tree, Depth-First Search, Array +1 |
| 2581 | Count Number of Possible Root Nodes | Hard | Tree, Depth-First Search, Array +2 |
| 2603 | Collect Coins in a Tree | Hard | Tree, Graph, Topological Sort +1 |
| 2646 | Minimize the Total Price of the Trips | Hard | Tree, Depth-First Search, Graph +2 |
| 2791 | Count Paths That Can Form a Palindrome in a Tree | Hard | Bit Manipulation, Tree, Depth-First Search +2 |
| 2846 | Minimum Edge Weight Equilibrium Queries in a Tree | Hard | Tree, Graph, Array +1 |
| 2867 | Count Valid Paths in a Tree | Hard | Tree, Depth-First Search, Math +2 |
| 2872 | Maximum Number of K-Divisible Components | Hard | Tree, Depth-First Search |
| 2920 | Maximum Points After Collecting Coins From All Nodes | Hard | Bit Manipulation, Tree, Depth-First Search +3 |
| 2973 | Find Number of Coins to Place in Tree Nodes | Hard | Tree, Depth-First Search, Dynamic Programming +2 |
| 272 | Closest Binary Search Tree Value IIPremium | Hard | Stack, Tree, Depth-First Search +4 |
| 428 | Serialize and Deserialize N-ary TreePremium | Hard | Tree, Depth-First Search, Breadth-First Search +1 |
| 431 | Encode N-ary Tree to Binary TreePremium | Hard | Tree, Depth-First Search, Breadth-First Search +2 |
| 1516 | Move Sub-Tree of N-Ary TreePremium | Hard | Tree, Depth-First Search |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Sorting392
- Greedy346
- Depth-First Search289
- Binary Search253
- Database249
- Breadth-First Search223
- Matrix216
- Two Pointers201
- Bit Manipulation194
- Binary Tree174
- Heap (Priority Queue)163
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Counting126
- Design122
- Sliding Window116
- Backtracking105
When the 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.