Skip to content
LEETFLIX

type

Tree LeetCode problems — visual solutions

Every LeetCode problem tagged Tree — the full catalogue, plus animated visual solutions in Python and Go when a deep-dive exists.

364 problems · 282 animated · 82 locked

Full Tree index

  1. 94Binary Tree Inorder TraversalWatch
  2. 95Unique Binary Search Trees IIWatch
  3. 96Unique Binary Search TreesWatch
  4. 98Validate Binary Search TreeWatch
  5. 99Recover Binary Search TreeWatch
  6. 100Same TreeWatch
  7. 101Symmetric TreeWatch
  8. 102Binary Tree Level Order TraversalWatch
  9. 103Binary Tree Zigzag Level Order TraversalWatch
  10. 104Maximum Depth of Binary TreeWatch
  11. 105Construct Binary Tree from Preorder and Inorder TraversalWatch
  12. 106Construct Binary Tree from Inorder and Postorder TraversalWatch
  13. 107Binary Tree Level Order Traversal IIWatch
  14. 108Convert Sorted Array to Binary Search TreeWatch
  15. 109Convert Sorted List to Binary Search TreeWatch
  16. 110Balanced Binary TreeWatch
  17. 111Minimum Depth of Binary TreeWatch
  18. 112Path SumWatch
  19. 113Path Sum IIWatch
  20. 114Flatten Binary Tree to Linked ListWatch
  21. 116Populating Next Right Pointers in Each NodeWatch
  22. 117Populating Next Right Pointers in Each Node IIWatch
  23. 124Binary Tree Maximum Path SumWatch
  24. 129Sum Root to Leaf NumbersWatch
  25. 144Binary Tree Preorder TraversalWatch
  26. 145Binary Tree Postorder TraversalWatch
  27. 156Binary Tree Upside DownLocked
  28. 173Binary Search Tree IteratorWatch
  29. 199Binary Tree Right Side ViewWatch
  30. 218The Skyline ProblemWatch
  31. 222Count Complete Tree NodesWatch
  32. 226Invert Binary TreeWatch
  33. 230Kth Smallest Element in a BSTWatch
  34. 235Lowest Common Ancestor of a Binary Search TreeWatch
  35. 236Lowest Common Ancestor of a Binary TreeWatch
  36. 250Count Univalue SubtreesLocked
  37. 255Verify Preorder Sequence in Binary Search TreeLocked
  38. 257Binary Tree PathsWatch
  39. 270Closest Binary Search Tree ValueLocked
  40. 272Closest Binary Search Tree Value IILocked
  41. 285Inorder Successor in BSTLocked
  42. 297Serialize and Deserialize Binary TreeWatch
  43. 298Binary Tree Longest Consecutive SequenceLocked
  44. 307Range Sum Query - MutableWatch
  45. 308Range Sum Query 2D - MutableLocked
  46. 314Binary Tree Vertical Order TraversalLocked
  47. 315Count of Smaller Numbers After SelfWatch
  48. 327Count of Range SumWatch
  49. 331Verify Preorder Serialization of a Binary TreeWatch
  50. 333Largest BST SubtreeLocked
  51. 337House Robber IIIWatch
  52. 341Flatten Nested List IteratorWatch
  53. 366Find Leaves of Binary TreeLocked
  54. 404Sum of Left LeavesWatch
  55. 406Queue Reconstruction by HeightWatch
  56. 426Convert Binary Search Tree to Sorted Doubly Linked ListLocked
  57. 427Construct Quad TreeWatch
  58. 428Serialize and Deserialize N-ary TreeLocked
  59. 429N-ary Tree Level Order TraversalWatch
  60. 431Encode N-ary Tree to Binary TreeLocked
  61. 437Path Sum IIIWatch
  62. 449Serialize and Deserialize BSTWatch
  63. 450Delete Node in a BSTWatch
  64. 493Reverse PairsWatch
  65. 501Find Mode in Binary Search TreeWatch
  66. 508Most Frequent Subtree SumWatch
  67. 510Inorder Successor in BST IILocked
  68. 513Find Bottom Left Tree ValueWatch
  69. 515Find Largest Value in Each Tree RowWatch
  70. 530Minimum Absolute Difference in BSTWatch
  71. 536Construct Binary Tree from StringLocked
  72. 538Convert BST to Greater TreeWatch
  73. 543Diameter of Binary TreeWatch
  74. 545Boundary of Binary TreeLocked
  75. 549Binary Tree Longest Consecutive Sequence IILocked
  76. 558Logical OR of Two Binary Grids Represented as Quad-TreesWatch
  77. 559Maximum Depth of N-ary TreeWatch
  78. 563Binary Tree TiltWatch
  79. 572Subtree of Another TreeWatch
  80. 582Kill ProcessLocked
  81. 589N-ary Tree Preorder TraversalWatch
  82. 590N-ary Tree Postorder TraversalWatch
  83. 606Construct String from Binary TreeWatch
  84. 617Merge Two Binary TreesWatch
  85. 623Add One Row to TreeWatch
  86. 637Average of Levels in Binary TreeWatch
  87. 652Find Duplicate SubtreesWatch
  88. 653Two Sum IV - Input is a BSTWatch
  89. 654Maximum Binary TreeWatch
  90. 655Print Binary TreeWatch
  91. 662Maximum Width of Binary TreeWatch
  92. 663Equal Tree PartitionLocked
  93. 666Path Sum IVLocked
  94. 669Trim a Binary Search TreeWatch
  95. 671Second Minimum Node In a Binary TreeWatch
  96. 673Number of Longest Increasing SubsequenceWatch
  97. 683K Empty SlotsLocked
  98. 687Longest Univalue PathWatch
  99. 690Employee ImportanceWatch
  100. 699Falling SquaresWatch
  101. 700Search in a Binary Search TreeWatch
  102. 701Insert into a Binary Search TreeWatch
  103. 703Kth Largest Element in a StreamWatch
  104. 715Range ModuleWatch
  105. 729My Calendar IWatch
  106. 731My Calendar IIWatch
  107. 732My Calendar IIIWatch
  108. 742Closest Leaf in a Binary TreeLocked
  109. 776Split BSTLocked
  110. 783Minimum Distance Between BST NodesWatch
  111. 814Binary Tree PruningWatch
  112. 834Sum of Distances in TreeWatch
  113. 850Rectangle Area IIWatch
  114. 863All Nodes Distance K in Binary TreeWatch
  115. 865Smallest Subtree with all the Deepest NodesWatch
  116. 872Leaf-Similar TreesWatch
  117. 889Construct Binary Tree from Preorder and Postorder TraversalWatch
  118. 894All Possible Full Binary TreesWatch
  119. 897Increasing Order Search TreeWatch
  120. 919Complete Binary Tree InserterWatch
  121. 938Range Sum of BSTWatch
  122. 951Flip Equivalent Binary TreesWatch
  123. 958Check Completeness of a Binary TreeWatch
  124. 965Univalued Binary TreeWatch
  125. 968Binary Tree CamerasWatch
  126. 971Flip Binary Tree To Match Preorder TraversalWatch
  127. 979Distribute Coins in Binary TreeWatch
  128. 987Vertical Order Traversal of a Binary TreeWatch
  129. 988Smallest String Starting From LeafWatch
  130. 993Cousins in Binary TreeWatch
  131. 998Maximum Binary Tree IIWatch
  132. 1008Construct Binary Search Tree from Preorder TraversalWatch
  133. 1022Sum of Root To Leaf Binary NumbersWatch
  134. 1026Maximum Difference Between Node and AncestorWatch
  135. 1028Recover a Tree From Preorder TraversalWatch
  136. 1038Binary Search Tree to Greater Sum TreeWatch
  137. 1080Insufficient Nodes in Root to Leaf PathsWatch
  138. 1104Path In Zigzag Labelled Binary TreeWatch
  139. 1110Delete Nodes And Return ForestWatch
  140. 1120Maximum Average SubtreeLocked
  141. 1123Lowest Common Ancestor of Deepest LeavesWatch
  142. 1145Binary Tree Coloring GameWatch
  143. 1157Online Majority Element In SubarrayWatch
  144. 1161Maximum Level Sum of a Binary TreeWatch
  145. 1214Two Sum BSTsLocked
  146. 1245Tree DiameterLocked
  147. 1257Smallest Common RegionLocked
  148. 1261Find Elements in a Contaminated Binary TreeWatch
  149. 1273Delete Tree NodesLocked
  150. 1302Deepest Leaves SumWatch
  151. 1305All Elements in Two Binary Search TreesWatch
  152. 1315Sum of Nodes with Even-Valued GrandparentWatch
  153. 1325Delete Leaves With a Given ValueWatch
  154. 1339Maximum Product of Splitted Binary TreeWatch
  155. 1361Validate Binary Tree NodesWatch
  156. 1367Linked List in Binary TreeWatch
  157. 1372Longest ZigZag Path in a Binary TreeWatch
  158. 1373Maximum Sum BST in Binary TreeWatch
  159. 1376Time Needed to Inform All EmployeesWatch
  160. 1377Frog Position After T SecondsWatch
  161. 1379Find a Corresponding Node of a Binary Tree in a Clone of That TreeWatch
  162. 1382Balance a Binary Search TreeWatch
  163. 1395Count Number of TeamsWatch
  164. 1409Queries on a Permutation With KeyWatch
  165. 1430Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary TreeLocked
  166. 1443Minimum Time to Collect All Apples in a TreeWatch
  167. 1448Count Good Nodes in Binary TreeWatch
  168. 1457Pseudo-Palindromic Paths in a Binary TreeWatch
  169. 1469Find All The Lonely NodesLocked
  170. 1483Kth Ancestor of a Tree NodeWatch
  171. 1485Clone Binary Tree With Random PointerLocked
  172. 1490Clone N-ary TreeLocked
  173. 1505Minimum Possible Integer After at Most K Adjacent Swaps On DigitsWatch
  174. 1506Find Root of N-Ary TreeLocked
  175. 1516Move Sub-Tree of N-Ary TreeLocked
  176. 1519Number of Nodes in the Sub-Tree With the Same LabelWatch
  177. 1521Find a Value of a Mysterious Function Closest to TargetWatch
  178. 1522Diameter of N-Ary TreeLocked
  179. 1530Number of Good Leaf Nodes PairsWatch
  180. 1569Number of Ways to Reorder Array to Get Same BSTWatch
  181. 1586Binary Search Tree Iterator IILocked
  182. 1597Build Binary Expression Tree From Infix ExpressionLocked
  183. 1600Throne InheritanceWatch
  184. 1602Find Nearest Right Node in Binary TreeLocked
  185. 1609Even Odd TreeWatch
  186. 1612Check If Two Expression Trees are EquivalentLocked
  187. 1617Count Subtrees With Max Distance Between CitiesWatch
  188. 1622Fancy SequenceWatch
  189. 1628Design an Expression Tree With Evaluate FunctionLocked
  190. 1644Lowest Common Ancestor of a Binary Tree IILocked
  191. 1649Create Sorted Array through InstructionsWatch
  192. 1650Lowest Common Ancestor of a Binary Tree IIILocked
  193. 1660Correct a Binary TreeLocked
  194. 1666Change the Root of a Binary TreeLocked
  195. 1676Lowest Common Ancestor of a Binary Tree IVLocked
  196. 1687Delivering Boxes from Storage to PortsWatch
  197. 1719Number Of Ways To Reconstruct A TreeWatch
  198. 1740Find Distance in a Binary TreeLocked
  199. 1766Tree of CoprimesWatch
  200. 1902Depth of BST Given Insertion OrderLocked
  201. 1916Count Ways to Build Rooms in an Ant ColonyWatch
  202. 1932Merge BSTs to Create Single BSTWatch
  203. 1964Find the Longest Valid Obstacle Course at Each PositionWatch
  204. 1973Count Nodes Equal to Sum of DescendantsLocked
  205. 1993Operations on TreeWatch
  206. 2003Smallest Missing Genetic Value in Each SubtreeWatch
  207. 2005Subtree Removal Game with Fibonacci TreeLocked
  208. 2031Count Subarrays With More Ones Than ZerosLocked
  209. 2049Count Nodes With the Highest ScoreWatch
  210. 2080Range Frequency QueriesWatch
  211. 2096Step-By-Step Directions From a Binary Tree Node to AnotherWatch
  212. 2158Amount of New Area Painted Each DayLocked
  213. 2179Count Good Triplets in an ArrayWatch
  214. 2193Minimum Number of Moves to Make PalindromeWatch
  215. 2196Create Binary Tree From DescriptionsWatch
  216. 2213Longest Substring of One Repeating CharacterWatch
  217. 2236Root Equals Sum of ChildrenWatch
  218. 2246Longest Path With Different Adjacent CharactersWatch
  219. 2250Count Number of Rectangles Containing Each PointWatch
  220. 2265Count Nodes Equal to Average of SubtreeWatch
  221. 2276Count Integers in IntervalsWatch
  222. 2277Closest Node to Path in TreeLocked
  223. 2286Booking Concert Tickets in GroupsWatch
  224. 2313Minimum Flips in Binary Tree to Get ResultLocked
  225. 2322Minimum Score After Removals on a TreeWatch
  226. 2331Evaluate Boolean Binary TreeWatch
  227. 2368Reachable Nodes With RestrictionsWatch
  228. 2378Choose Edges to Maximize Score in a TreeLocked
  229. 2385Amount of Time for Binary Tree to Be InfectedWatch
  230. 2407Longest Increasing Subsequence IIWatch
  231. 2415Reverse Odd Levels of Binary TreeWatch
  232. 2421Number of Good PathsWatch
  233. 2424Longest Uploaded PrefixWatch
  234. 2426Number of Pairs Satisfying InequalityWatch
  235. 2440Create Components With Same ValueWatch
  236. 2445Number of Nodes With Value OneLocked
  237. 2458Height of Binary Tree After Subtree Removal QueriesWatch
  238. 2467Most Profitable Path in a TreeWatch
  239. 2471Minimum Number of Operations to Sort a Binary Tree by LevelWatch
  240. 2476Closest Nodes Queries in a Binary Search TreeWatch
  241. 2477Minimum Fuel Cost to Report to the CapitalWatch
  242. 2479Maximum XOR of Two Non-Overlapping SubtreesLocked
  243. 2509Cycle Length Queries in a TreeWatch
  244. 2519Count the Number of K-Big IndicesLocked
  245. 2538Difference Between Maximum and Minimum Price SumWatch
  246. 2552Count Increasing QuadrupletsWatch
  247. 2569Handling Sum Queries After UpdateWatch
  248. 2581Count Number of Possible Root NodesWatch
  249. 2583Kth Largest Sum in a Binary TreeWatch
  250. 2603Collect Coins in a TreeWatch
  251. 2641Cousins in Binary Tree IIWatch
  252. 2646Minimize the Total Price of the TripsWatch
  253. 2659Make Array EmptyWatch
  254. 2673Make Costs of Paths Equal in a Binary TreeWatch
  255. 2689Extract Kth Character From The Rope TreeLocked
  256. 2736Maximum Sum QueriesWatch
  257. 2764Is Array a Preorder of Some ‌Binary TreeLocked
  258. 2773Height of Special Binary TreeLocked
  259. 2791Count Paths That Can Form a Palindrome in a TreeWatch
  260. 2792Count Nodes That Are Great EnoughLocked
  261. 2846Minimum Edge Weight Equilibrium Queries in a TreeWatch
  262. 2867Count Valid Paths in a TreeWatch
  263. 2872Maximum Number of K-Divisible ComponentsWatch
  264. 2907Maximum Profitable Triplets With Increasing Prices ILocked
  265. 2913Subarrays Distinct Element Sum of Squares IWatch
  266. 2916Subarrays Distinct Element Sum of Squares IIWatch
  267. 2920Maximum Points After Collecting Coins From All NodesWatch
  268. 2921Maximum Profitable Triplets With Increasing Prices IILocked
  269. 2925Maximum Score After Applying Operations on a TreeWatch
  270. 2926Maximum Balanced Subsequence SumWatch
  271. 2940Find Building Where Alice and Bob Can MeetWatch
  272. 2973Find Number of Coins to Place in Tree NodesWatch
  273. 3004Maximum Subtree of the Same ColorLocked
  274. 3009Maximum Number of Intersections on the ChartLocked
  275. 3067Count Pairs of Connectable Servers in a Weighted Tree NetworkWatch
  276. 3068Find the Maximum Sum of Node ValuesWatch
  277. 3072Distribute Elements Into Two Arrays IIWatch
  278. 3109Find the Index of PermutationLocked
  279. 3117Minimum Sum of Values by Dividing ArrayWatch
  280. 3157Find the Level of Tree with Minimum SumLocked
  281. 3161Block Placement QueriesWatch
  282. 3165Maximum Sum of Subsequence With Non-adjacent ElementsWatch
  283. 3171Find Subarray With Bitwise OR Closest to KWatch
  284. 3187Peaks in ArrayWatch
  285. 3203Find Minimum Diameter After Merging Two TreesWatch
  286. 3209Number of Subarrays With AND Value of KWatch
  287. 3241Time Taken to Mark All NodesWatch
  288. 3245Alternating Groups IIIWatch
  289. 3249Count the Number of Good NodesWatch
  290. 3291Minimum Number of Valid Strings to Form Target IWatch
  291. 3292Minimum Number of Valid Strings to Form Target IIWatch
  292. 3313Find the Last Marked Nodes in TreeLocked
  293. 3319K-th Largest Perfect Subtree Size in Binary TreeWatch
  294. 3327Check if DFS Strings Are PalindromesWatch
  295. 3331Find Subtree Sizes After ChangesWatch
  296. 3367Maximize Sum of Weights after Edge RemovalsWatch
  297. 3372Maximize the Number of Target Nodes After Connecting Trees IWatch
  298. 3373Maximize the Number of Target Nodes After Connecting Trees IIWatch
  299. 3380Maximum Area Rectangle With Point Constraints IWatch
  300. 3382Maximum Area Rectangle With Point Constraints IIWatch
  301. 3410Maximize Subarray Sum After Removing All Occurrences of One ElementWatch
  302. 3420Count Non-Decreasing Subarrays After K OperationsWatch
  303. 3425Longest Special PathWatch
  304. 3454Separate Squares IIWatch
  305. 3477Fruits Into Baskets IIWatch
  306. 3479Fruits Into Baskets IIIWatch
  307. 3480Maximize Subarrays After Removing One Conflicting PairWatch
  308. 3486Longest Special Path IIWatch
  309. 3501Maximize Active Section with Trade IIWatch
  310. 3515Shortest Path in a Weighted TreeWatch
  311. 3520Minimum Threshold for Inversion Pairs CountLocked
  312. 3525Find X Value of Array IIWatch
  313. 3526Range XOR Queries with Subarray ReversalsLocked
  314. 3544Subtree Inversion SumWatch
  315. 3553Minimum Weighted Subgraph With the Required Paths IIWatch
  316. 3558Number of Ways to Assign Edge Weights IWatch
  317. 3559Number of Ways to Assign Edge Weights IIWatch
  318. 3562Maximum Profit from Trading Stocks with DiscountsWatch
  319. 3569Maximize Count of Distinct Primes After SplitWatch
  320. 3575Maximum Good Subtree ScoreWatch
  321. 3585Find Weighted Median Node in TreeWatch
  322. 3590Kth Smallest Path XOR SumWatch
  323. 3593Minimum Increments to Equalize Leaf PathsWatch
  324. 3605Minimum Stability Factor of ArrayWatch
  325. 3624Number of Integers With Popcount-Depth Equal to K IIWatch
  326. 3671Sum of Beautiful SubsequencesWatch
  327. 3691Maximum Total Subarray Value IIWatch
  328. 3715Sum of Perfect Square AncestorsWatch
  329. 3719Longest Balanced Subarray IWatch
  330. 3721Longest Balanced Subarray IIWatch
  331. 3737Count Subarrays With Majority Element IWatch
  332. 3739Count Subarrays With Majority Element IIWatch
  333. 3762Minimum Operations to Equalize SubarraysWatch
  334. 3768Minimum Inversion Count in Subarrays of Fixed LengthWatch
  335. 3772Maximum Subgraph Score in a TreeWatch
  336. 3777Minimum Deletions to Make Alternating SubstringWatch
  337. 3786Total Sum of Interaction Cost in Tree GroupsWatch
  338. 3787Find Diameter Endpoints of a TreeLocked
  339. 3812Minimum Edge Toggles on a TreeWatch
  340. 3820Pythagorean Distance Nodes in a TreeWatch
  341. 3831Median of a Binary Search Tree LevelLocked
  342. 3841Palindromic Path Queries in a TreeWatch
  343. 3879Maximum Distinct Path Sum in a Binary TreeLocked
  344. 3901Good Subsequence QueriesWatch
  345. 3902Zigzag Level Sum of Binary TreeLocked
  346. 3915Maximum Sum of Alternating Subsequence With Distance at Least KWatch
  347. 3930Power Update After K-th Largest Insertion IILocked
  348. 3935Power Update After K-th Largest Insertion ILocked
  349. 3939Count Non Adjacent Subsets in a Rooted TreeWatch
  350. 3949Subtree Inversion Sum IILocked
  351. 3962Maximum Subarray Sum After at Most K SwapsWatch
  352. 3965Finish Time of Tasks IWatch
  353. 3967Finish Time of Tasks IILocked
  354. 3973Distinct Gate Paths to LCALocked
  355. 3997Count Dominant Nodes in a Binary TreeWatch
  356. 4011Count Subarrays With Even Odd Ratio IWatch
  357. 4013Count Subarrays With Even Odd Ratio IIWatch
  358. 4015Weighted Sum of a TreeWatch
  359. 4017Peaks in Array IIWatch
  360. 4018Total Sum of Interaction Cost in Tree Groups IILocked
  361. 4033Valid K-Unique Subarrays IWatch
  362. 4042Valid K-Unique Subarrays IILocked
  363. 4051Count Subarrays with Distant SumsWatch
  364. 4055Count Shadow Pairs IIWatch