Tree LeetCode Problems: All 225,
With Python Solutions

Every problem in this library that LeetCode tags Tree. 167 of them carry a complete Python solution, a worked example and the time and space complexity of the approach.

Easy
41
Medium
142
Hard
42
1 pattern

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.

Eyes only

When the Tree problem arrives live.

In the live round, Stealth Interview reads the 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 says. It's a desktop app for macOS and Windows, invisible to screen sharing.

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)

Easy problems
No.Problem
0094Binary Tree Inorder Traversal
0100Same Tree
0101Symmetric Tree
0104Maximum Depth of Binary Tree
0108Convert Sorted Array to Binary Search Tree
0110Balanced Binary Tree
0111Minimum Depth of Binary Tree
0112Path Sum
0144Binary Tree Preorder Traversal
0145Binary Tree Postorder Traversal
0222Count Complete Tree Nodes
0226Invert Binary Tree
0257Binary Tree Paths
0404Sum of Left Leaves
0501Find Mode in Binary Search Tree
0530Minimum Absolute Difference in BST
0543Diameter of Binary Tree
0559Maximum Depth of N-ary Tree
0563Binary Tree Tilt
0572Subtree of Another Tree
0589N-ary Tree Preorder Traversal
0590N-ary Tree Postorder Traversal
0617Merge Two Binary Trees
0637Average of Levels in Binary Tree
0653Two Sum IV - Input is a BST
0671Second Minimum Node In a Binary Tree
0700Search in a Binary Search Tree
0703Kth Largest Element in a Stream
0783Minimum Distance Between BST Nodes
0872Leaf-Similar Trees
0897Increasing Order Search Tree
0938Range Sum of BST
0965Univalued Binary Tree
0993Cousins in Binary Tree
1022Sum of Root To Leaf Binary Numbers
1379Find a Corresponding Node of a Binary Tree in a Clone of That Tree
2236Root Equals Sum of Children
2331Evaluate Boolean Binary Tree
0270Closest Binary Search Tree ValuePremium
1469Find All The Lonely NodesPremium

Medium (125)

Medium problems
No.Problem
0095Unique Binary Search Trees II
0096Unique Binary Search Trees
0098Validate Binary Search Tree
0099Recover Binary Search Tree
0102Binary Tree Level Order Traversal
0103Binary Tree Zigzag Level Order Traversal
0105Construct Binary Tree from Preorder and Inorder Traversal
0106Construct Binary Tree from Inorder and Postorder Traversal
0107Binary Tree Level Order Traversal II
0109Convert Sorted List to Binary Search Tree
0113Path Sum II
0114Flatten Binary Tree to Linked List
0116Populating Next Right Pointers in Each Node
0117Populating Next Right Pointers in Each Node II
0129Sum Root to Leaf Numbers
0173Binary Search Tree Iterator
0199Binary Tree Right Side View
0230Kth Smallest Element in a BST
0235Lowest Common Ancestor of a Binary Search Tree
0236Lowest Common Ancestor of a Binary Tree
0331Verify Preorder Serialization of a Binary Tree
0337House Robber III
0341Flatten Nested List Iterator
0427Construct Quad Tree
0429N-ary Tree Level Order Traversal
0437Path Sum III
0449Serialize and Deserialize BST
0450Delete Node in a BST
0508Most Frequent Subtree Sum
0513Find Bottom Left Tree Value
0515Find Largest Value in Each Tree Row
0538Convert BST to Greater Tree
0558Logical OR of Two Binary Grids Represented as Quad-Trees
0606Construct String from Binary Tree
0623Add One Row to Tree
0652Find Duplicate Subtrees
0654Maximum Binary Tree
0655Print Binary Tree
0662Maximum Width of Binary Tree
0669Trim a Binary Search Tree
0687Longest Univalue Path
0690Employee Importance
0701Insert into a Binary Search Tree
0814Binary Tree Pruning
0863All Nodes Distance K in Binary Tree
0865Smallest Subtree with all the Deepest Nodes
0889Construct Binary Tree from Preorder and Postorder Traversal
0894All Possible Full Binary Trees
0919Complete Binary Tree Inserter
0951Flip Equivalent Binary Trees
0958Check Completeness of a Binary Tree
0971Flip Binary Tree To Match Preorder Traversal
0979Distribute Coins in Binary Tree
0988Smallest String Starting From Leaf
0998Maximum Binary Tree II
1008Construct Binary Search Tree from Preorder Traversal
1026Maximum Difference Between Node and Ancestor
1038Binary Search Tree to Greater Sum Tree
1080Insufficient Nodes in Root to Leaf Paths
1104Path In Zigzag Labelled Binary Tree
1110Delete Nodes And Return Forest
1123Lowest Common Ancestor of Deepest Leaves
1145Binary Tree Coloring Game
1161Maximum Level Sum of a Binary Tree
1261Find Elements in a Contaminated Binary Tree
1302Deepest Leaves Sum
1305All Elements in Two Binary Search Trees
1315Sum of Nodes with Even-Valued Grandparent
1325Delete Leaves With a Given Value
1339Maximum Product of Splitted Binary Tree
1361Validate Binary Tree Nodes
1367Linked List in Binary Tree
1372Longest ZigZag Path in a Binary Tree
1376Time Needed to Inform All Employees
1382Balance a Binary Search Tree
1443Minimum Time to Collect All Apples in a Tree
1448Count Good Nodes in Binary Tree
1457Pseudo-Palindromic Paths in a Binary Tree
1519Number of Nodes in the Sub-Tree With the Same Label
1530Number of Good Leaf Nodes Pairs
1600Throne Inheritance
1609Even Odd Tree
1993Operations on Tree
2049Count Nodes With the Highest Score
2096Step-By-Step Directions From a Binary Tree Node to Another
2196Create Binary Tree From Descriptions
2265Count Nodes Equal to Average of Subtree
2368Reachable Nodes With Restrictions
2385Amount of Time for Binary Tree to Be Infected
2415Reverse Odd Levels of Binary Tree
2467Most Profitable Path in a Tree
2471Minimum Number of Operations to Sort a Binary Tree by Level
2476Closest Nodes Queries in a Binary Search Tree
2477Minimum Fuel Cost to Report to the Capital
2583Kth Largest Sum in a Binary Tree
2641Cousins in Binary Tree II
2673Make Costs of Paths Equal in a Binary Tree
2925Maximum Score After Applying Operations on a Tree
0156Binary Tree Upside DownPremium
0250Count Univalue SubtreesPremium
0255Verify Preorder Sequence in Binary Search TreePremium
0285Inorder Successor in BSTPremium
0298Binary Tree Longest Consecutive SequencePremium
0314Binary Tree Vertical Order TraversalPremium
0333Largest BST SubtreePremium
0366Find Leaves of Binary TreePremium
0426Convert Binary Search Tree to Sorted Doubly Linked ListPremium
0510Inorder Successor in BST IIPremium
0536Construct Binary Tree from StringPremium
0545Boundary of Binary TreePremium
0549Binary Tree Longest Consecutive Sequence IIPremium
0582Kill ProcessPremium
0663Equal Tree PartitionPremium
0666Path Sum IVPremium
0742Closest Leaf in a Binary TreePremium
0776Split BSTPremium
1120Maximum Average SubtreePremium
1214Two Sum BSTsPremium
1245Tree DiameterPremium
1257Smallest Common RegionPremium
1273Delete Tree NodesPremium
1430Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary TreePremium
1485Clone Binary Tree With Random PointerPremium
1490Clone N-ary TreePremium
1506Find Root of N-Ary TreePremium

Hard (35)

Hard problems
No.Problem
0124Binary Tree Maximum Path Sum
0297Serialize and Deserialize Binary Tree
0834Sum of Distances in Tree
0968Binary Tree Cameras
0987Vertical Order Traversal of a Binary Tree
1028Recover a Tree From Preorder Traversal
1373Maximum Sum BST in Binary Tree
1377Frog Position After T Seconds
1483Kth Ancestor of a Tree Node
1569Number of Ways to Reorder Array to Get Same BST
1617Count Subtrees With Max Distance Between Cities
1719Number Of Ways To Reconstruct A Tree
1766Tree of Coprimes
1916Count Ways to Build Rooms in an Ant Colony
2003Smallest Missing Genetic Value in Each Subtree
2246Longest Path With Different Adjacent Characters
2322Minimum Score After Removals on a Tree
2421Number of Good Paths
2440Create Components With Same Value
2458Height of Binary Tree After Subtree Removal Queries
2509Cycle Length Queries in a Tree
2538Difference Between Maximum and Minimum Price Sum
2581Count Number of Possible Root Nodes
2603Collect Coins in a Tree
2646Minimize the Total Price of the Trips
2791Count Paths That Can Form a Palindrome in a Tree
2846Minimum Edge Weight Equilibrium Queries in a Tree
2867Count Valid Paths in a Tree
2872Maximum Number of K-Divisible Components
2920Maximum Points After Collecting Coins From All Nodes
2973Find Number of Coins to Place in Tree Nodes
0272Closest Binary Search Tree Value IIPremium
0428Serialize and Deserialize N-ary TreePremium
0431Encode N-ary Tree to Binary TreePremium
1516Move Sub-Tree of N-Ary TreePremium