Tree Traversal Pattern: Template + 225 LeetCode Problems
Choose the order — preorder, inorder, postorder, level — and the problem solves itself.
- 41 Easy
- 142 Medium
- 42 Hard
- O(n) time
What the tree traversal pattern is
Almost every binary tree problem is a traversal with a small amount of work attached, so the real decision is when the node gets processed relative to its children. Preorder handles a node before descending and is what you want for copying, serialising, or passing information down from the root. Postorder handles it after both children return and is what you want whenever the answer at a node is computed from its subtrees — heights, diameters, subtree sums, the maximum path sum. Inorder is the special one: on a binary search tree it emits the values in sorted order, which turns validation, the kth smallest, and finding the minimum gap into a single scan with a pointer to the previously visited value. Level order is breadth-first search on a tree, done by draining the queue one full level at a time so each level can be handled as a unit. The iterative inorder is the traversal worth being able to write from memory, because it is the one an interviewer will ask for when recursion is ruled out.
When to use it
- A value at each node depends on its subtrees — that is postorder.
- The tree is a BST and the question is about sorted order, a kth element, or a range.
- The answer is per level, or is the shallowest node satisfying a condition.
- You are asked to serialise, clone, or rebuild a tree from traversal output.
The tree traversal template in Python
The shape, not a solution to any one problem. Adapt the condition and the summary being maintained; the skeleton stays the same across the 225 problems listed below.
def inorder(root):
result, stack, node = [], [], root
while node or stack:
while node: # descend as far left as possible
stack.append(node)
node = node.left
node = stack.pop()
result.append(node.val) # left subtree finished, so visit now
node = node.right # then do the same for the right subtree
return resultComplexity characteristics
- Time
- O(n)
- Auxiliary space
- O(h), where h is the height of the tree
Every node is visited exactly once whatever the order, so the time is linear in the node count. The space is the recursion stack or the explicit stack, which is the height of the tree: O(log n) on a balanced tree and O(n) on a degenerate one, and the degenerate one is what a test harness will hand you. Level-order traversal swaps the stack for a queue holding the widest level, which is up to n/2 nodes on a full tree.
All 225 tree traversal LeetCode problems
Every problem in the library the tree traversal pattern applies to, grouped by LeetCode's own difficulty rating. 167 of the 225 carry a complete Python solution with a worked example and complexity analysis; the rest are listed for completeness, with the LeetCode Premium ones marked.
Easy (41)
| # | 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 |
| 872 | Leaf-Similar Trees | Easy | Tree, Depth-First Search, Binary Tree |
| 270 | Closest Binary Search Tree ValuePremium | Easy | Tree, Depth-First Search, Binary Search Tree +2 |
| 783 | Minimum Distance Between BST Nodes | Easy | Tree, Depth-First Search, Breadth-First Search +2 |
| 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 |
| 1469 | Find All The Lonely NodesPremium | 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 |
| 2689 | Extract Kth Character From The Rope TreePremium | Easy | Tree, Depth-First Search, Binary Tree |
Medium (142)
| # | 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 |
| 1161 | Maximum Level Sum of a Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1372 | Longest ZigZag Path in a Binary Tree | Medium | Tree, Depth-First Search, Dynamic Programming +1 |
| 1448 | Count Good Nodes in Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 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 |
| 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 |
| 1120 | Maximum Average SubtreePremium | Medium | Tree, Depth-First Search, Binary Tree |
| 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 |
| 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 |
| 1261 | Find Elements in a Contaminated Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 1273 | Delete Tree NodesPremium | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 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 |
| 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 |
| 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 |
| 1443 | Minimum Time to Collect All Apples in a 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 |
| 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 |
| 1519 | Number of Nodes in the Sub-Tree With the Same Label | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1522 | Diameter of N-Ary TreePremium | Medium | Tree, Depth-First Search |
| 1530 | Number of Good Leaf Nodes Pairs | Medium | Tree, Depth-First Search, Binary Tree |
| 1586 | Binary Search Tree Iterator IIPremium | Medium | Stack, Tree, Design +3 |
| 1600 | Throne Inheritance | Medium | Tree, Depth-First Search, Design +1 |
| 1602 | Find Nearest Right Node in Binary TreePremium | Medium | Tree, Breadth-First Search, Binary Tree |
| 1609 | Even Odd Tree | 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 |
| 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 |
| 2378 | Choose Edges to Maximize Score in a TreePremium | Medium | Tree, Depth-First Search, Dynamic Programming |
| 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 |
| 2445 | Number of Nodes With Value OnePremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 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 |
| 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 |
| 2925 | Maximum Score After Applying Operations on a Tree | Medium | Tree, Depth-First Search, Dynamic Programming |
Hard (42)
| # | 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 |
| 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 |
| 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 |
| 1516 | Move Sub-Tree of N-Ary TreePremium | Hard | Tree, Depth-First Search |
| 1569 | Number of Ways to Reorder Array to Get Same BST | Hard | Tree, Union Find, Binary Search Tree +7 |
| 1597 | Build Binary Expression Tree From Infix ExpressionPremium | Hard | Stack, Tree, String +1 |
| 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 |
| 1932 | Merge BSTs to Create Single BST | Hard | Tree, Depth-First Search, Hash Table +2 |
| 2003 | Smallest Missing Genetic Value in Each Subtree | Hard | Tree, Depth-First Search, Union Find +1 |
| 2005 | Subtree Removal Game with Fibonacci TreePremium | Hard | Tree, Math, Dynamic Programming +2 |
| 2246 | Longest Path With Different Adjacent Characters | Hard | Tree, Depth-First Search, Graph +3 |
| 2277 | Closest Node to Path in TreePremium | Hard | Tree, Depth-First Search, Breadth-First Search +1 |
| 2313 | Minimum Flips in Binary Tree to Get ResultPremium | Hard | Tree, Depth-First Search, Dynamic Programming +1 |
| 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 |
| 2479 | Maximum XOR of Two Non-Overlapping SubtreesPremium | Hard | Tree, Depth-First Search, Graph +1 |
| 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 |
| 2792 | Count Nodes That Are Great EnoughPremium | Hard | Tree, Depth-First Search, Divide and Conquer +1 |
| 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 |
Related patterns
Problems sit in more than one pattern more often than not, and the overlap is where the interesting follow-up questions live.
Tree Traversal pattern FAQ
What is the tree traversal pattern?
Almost every binary tree problem is a traversal with a small amount of work attached, so the real decision is when the node gets processed relative to its children.
How many LeetCode problems use the tree traversal pattern?
This page lists 225 LeetCode problems that the tree traversal pattern applies to: 41 Easy, 142 Medium and 42 Hard. 167 of them carry a complete Python solution with complexity analysis.
What is the time complexity of the tree traversal pattern?
O(n) time and O(h), where h is the height of the tree space. Every node is visited exactly once whatever the order, so the time is linear in the node count. The space is the recursion stack or the explicit stack, which is the height of the tree: O(log n) on a balanced tree and O(n) on a degenerate one, and the degenerate one is what a test harness will hand you. Level-order traversal swaps the stack for a queue holding the widest level, which is up to n/2 nodes on a full tree.
When should I use the tree traversal pattern in an interview?
A value at each node depends on its subtrees — that is postorder. The tree is a BST and the question is about sorted order, a kth element, or a range.
Which tree traversal problem should I start with?
LeetCode 94. Binary Tree Inorder Traversal is the lowest-numbered Easy problem on this page, which makes it the usual starting point: the technique is visible without the problem's own complications getting in the way.
What patterns are related to tree traversal?
Depth-First Search, Breadth-First Search, Backtracking, Trie. Problems frequently sit in more than one of these, and the overlap is where the interesting follow-up questions come from.
More ways in: all 22 patterns, the curated study lists, or the full problem list.
Meet the tree traversal problem you did not practise
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.