Binary Tree LeetCode Problems: All 174,
With Python Solutions

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

Easy
38
Medium
120
Hard
16
1 pattern

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.

Eyes only

When the Binary 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.

Binary Tree problems by difficulty

Problems with a complete Python solution are listed first, then by ascending problem number.

Easy (38)

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
0563Binary Tree Tilt
0572Subtree of Another Tree
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
2689Extract Kth Character From The Rope TreePremium

Medium (120)

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
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
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
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
1382Balance a Binary Search Tree
1448Count Good Nodes in Binary Tree
1457Pseudo-Palindromic Paths in a Binary Tree
1530Number of Good Leaf Nodes Pairs
1609Even Odd 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
2385Amount of Time for Binary Tree to Be Infected
2415Reverse Odd Levels of Binary Tree
2471Minimum Number of Operations to Sort a Binary Tree by Level
2476Closest Nodes Queries in a Binary Search Tree
2583Kth Largest Sum in a Binary Tree
2641Cousins in Binary Tree II
2673Make Costs of Paths Equal in a Binary 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
0663Equal Tree PartitionPremium
0666Path Sum IVPremium
0742Closest Leaf in a Binary TreePremium
0776Split BSTPremium
1120Maximum Average SubtreePremium
1214Two Sum BSTsPremium
1430Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary TreePremium
1485Clone Binary Tree With Random PointerPremium
1586Binary Search Tree Iterator IIPremium
1602Find Nearest Right Node in Binary TreePremium
1612Check If Two Expression Trees are EquivalentPremium
1628Design an Expression Tree With Evaluate FunctionPremium
1644Lowest Common Ancestor of a Binary Tree IIPremium
1650Lowest Common Ancestor of a Binary Tree IIIPremium
1660Correct a Binary TreePremium
1666Change the Root of a Binary TreePremium
1676Lowest Common Ancestor of a Binary Tree IVPremium
1740Find Distance in a Binary TreePremium
1902Depth of BST Given Insertion OrderPremium
1973Count Nodes Equal to Sum of DescendantsPremium
2445Number of Nodes With Value OnePremium
2764Is Array a Preorder of Some ‌Binary TreePremium
2773Height of Special Binary TreePremium

Hard (16)

Hard problems
No.Problem
0124Binary Tree Maximum Path Sum
0297Serialize and Deserialize Binary Tree
0968Binary Tree Cameras
0987Vertical Order Traversal of a Binary Tree
1028Recover a Tree From Preorder Traversal
1373Maximum Sum BST in Binary Tree
1569Number of Ways to Reorder Array to Get Same BST
2458Height of Binary Tree After Subtree Removal Queries
2509Cycle Length Queries in a Tree
0272Closest Binary Search Tree Value IIPremium
0431Encode N-ary Tree to Binary TreePremium
1597Build Binary Expression Tree From Infix ExpressionPremium
1932Merge BSTs to Create Single BST
2005Subtree Removal Game with Fibonacci TreePremium
2313Minimum Flips in Binary Tree to Get ResultPremium
2792Count Nodes That Are Great EnoughPremium