Tree Problems

Master tree problems with AI-powered solutions. Get instant coding assistance during your technical interviews for all 159 problems in this category.

Total Problems: 159
Easy: 38
Medium: 87
Hard: 34
Showing 50 of 159 problems

Problems

Scroll within this area to browse all problems

#
Title
Difficulty
144

Binary Tree Preorder Traversal

Easy
94

Binary Tree Inorder Traversal

Easy
226

Invert Binary Tree

Easy
236

Lowest Common Ancestor of a Binary Tree

Medium
145

Binary Tree Postorder Traversal

Easy
102

Binary Tree Level Order Traversal

Medium
98

Validate Binary Search Tree

Medium
110

Balanced Binary Tree

Easy
543

Diameter of Binary Tree

Easy
199

Binary Tree Right Side View

Medium
104

Maximum Depth of Binary Tree

Easy
124

Binary Tree Maximum Path Sum

Hard
1029

Vertical Order Traversal of a Binary Tree

Hard
222

Count Complete Tree Nodes

Easy
235

Lowest Common Ancestor of a Binary Search Tree

Medium
108

Convert Sorted Array to Binary Search Tree

Easy
450

Delete Node in a BST

Medium
105

Construct Binary Tree from Preorder and Inorder Traversal

Medium
114

Flatten Binary Tree to Linked List

Medium
653

Two Sum IV - Input is a BST

Easy
106

Construct Binary Tree from Inorder and Postorder Traversal

Medium
297

Serialize and Deserialize Binary Tree

Hard
572

Subtree of Another Tree

Easy
230

Kth Smallest Element in a BST

Medium
784

Insert into a Binary Search Tree

Medium
103

Binary Tree Zigzag Level Order Traversal

Medium
129

Sum Root to Leaf Numbers

Medium
101

Symmetric Tree

Easy
341

Flatten Nested List Iterator

Medium
617

Merge Two Binary Trees

Easy
111

Minimum Depth of Binary Tree

Easy
96

Unique Binary Search Trees

Medium
1285

Balance a Binary Search Tree

Medium
893

All Nodes Distance K in Binary Tree

Medium
116

Populating Next Right Pointers in Each Node

Medium
99

Recover Binary Search Tree

Medium
783

Search in a Binary Search Tree

Easy
107

Binary Tree Level Order Traversal II

Medium
975

Range Sum of BST

Easy
530

Minimum Absolute Difference in BST

Easy
173

Binary Search Tree Iterator

Medium
513

Find Bottom Left Tree Value

Medium
764

N-ary Tree Level Order Traversal

Medium
1544

Count Good Nodes in Binary Tree

Medium
1035

Cousins in Binary Tree

Easy
109

Convert Sorted List to Binary Search Tree

Medium
789

Kth Largest Element in a Stream

Easy
95

Unique Binary Search Trees II

Medium
1021

Distribute Coins in Binary Tree

Medium
2558

Minimum Number of Operations to Sort a Binary Tree by Level

Medium
Loading more problems...

Tree LeetCode Problems List

  • 144. Binary Tree Preorder Traversal - Easy - TreeGiven the root of a binary tree, return the preorder traversal of its nodes' values.... Topics: Stack, Tree, Depth-First Search, Binary Tree
  • 94. Binary Tree Inorder Traversal - Easy - TreeGiven the root of a binary tree, return the inorder traversal of its nodes' values.... Topics: Stack, Tree, Depth-First Search, Binary Tree
  • 226. Invert Binary Tree - Easy - TreeGiven the root of a binary tree, invert the tree, and return its root.... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 236. Lowest Common Ancestor of a Binary Tree - Medium - TreeGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowes... Topics: Tree, Depth-First Search, Binary Tree
  • 145. Binary Tree Postorder Traversal - Easy - TreeGiven the root of a binary tree, return the postorder traversal of its nodes' values.... Topics: Stack, Tree, Depth-First Search, Binary Tree
  • 102. Binary Tree Level Order Traversal - Medium - TreeGiven the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).... Topics: Tree, Breadth-First Search, Binary Tree
  • 98. Validate Binary Search Tree - Medium - TreeGiven the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: The left subtree of a node co... Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 110. Balanced Binary Tree - Easy - TreeGiven a binary tree, determine if it is height-balanced.... Topics: Tree, Depth-First Search, Binary Tree
  • 543. Diameter of Binary Tree - Easy - TreeGiven the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path betwee... Topics: Tree, Depth-First Search, Binary Tree
  • 199. Binary Tree Right Side View - Medium - TreeGiven the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bot... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 104. Maximum Depth of Binary Tree - Easy - TreeGiven the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root n... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 124. Binary Tree Maximum Path Sum - Hard - TreeA path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear i... Topics: Dynamic Programming, Tree, Depth-First Search, Binary Tree
  • 1029. Vertical Order Traversal of a Binary Tree - Hard - TreeGiven the root of a binary tree, calculate the vertical order traversal of the binary tree. For each node at position (row, col), its left and right c... Topics: Hash Table, Tree, Depth-First Search, Breadth-First Search, Sorting, Binary Tree
  • 222. Count Complete Tree Nodes - Easy - TreeGiven the root of a complete binary tree, return the number of the nodes in the tree. According to Wikipedia, every level, except possibly the last, i... Topics: Binary Search, Bit Manipulation, Tree, Binary Tree
  • 235. Lowest Common Ancestor of a Binary Search Tree - Medium - TreeGiven a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST. According to the definition of LCA on Wiki... Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 108. Convert Sorted Array to Binary Search Tree - Easy - TreeGiven an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.... Topics: Array, Divide and Conquer, Tree, Binary Search Tree, Binary Tree
  • 450. Delete Node in a BST - Medium - TreeGiven a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of th... Topics: Tree, Binary Search Tree, Binary Tree
  • 105. Construct Binary Tree from Preorder and Inorder Traversal - Medium - TreeGiven two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the sa... Topics: Array, Hash Table, Divide and Conquer, Tree, Binary Tree
  • 114. Flatten Binary Tree to Linked List - Medium - TreeGiven the root of a binary tree, flatten the tree into a "linked list": The "linked list" should use the same TreeNode class where the right child poi... Topics: Linked List, Stack, Tree, Depth-First Search, Binary Tree
  • 653. Two Sum IV - Input is a BST - Easy - TreeGiven the root of a binary search tree and an integer k, return true if there exist two elements in the BST such that their sum is equal to k, or fals... Topics: Hash Table, Two Pointers, Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree
  • 106. Construct Binary Tree from Inorder and Postorder Traversal - Medium - TreeGiven two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the... Topics: Array, Hash Table, Divide and Conquer, Tree, Binary Tree
  • 297. Serialize and Deserialize Binary Tree - Hard - TreeSerialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or ... Topics: String, Tree, Depth-First Search, Breadth-First Search, Design, Binary Tree
  • 572. Subtree of Another Tree - Easy - TreeGiven the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and... Topics: Tree, Depth-First Search, String Matching, Binary Tree, Hash Function
  • 230. Kth Smallest Element in a BST - Medium - TreeGiven the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.... Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 784. Insert into a Binary Search Tree - Medium - TreeYou are given the root node of a binary search tree (BST) and a value to insert into the tree. Return the root node of the BST after the insertion. It... Topics: Tree, Binary Search Tree, Binary Tree
  • 103. Binary Tree Zigzag Level Order Traversal - Medium - TreeGiven the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the n... Topics: Tree, Breadth-First Search, Binary Tree
  • 129. Sum Root to Leaf Numbers - Medium - TreeYou are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number. For example, the r... Topics: Tree, Depth-First Search, Binary Tree
  • 101. Symmetric Tree - Easy - TreeGiven the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 341. Flatten Nested List Iterator - Medium - TreeYou are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Im... Topics: Stack, Tree, Depth-First Search, Design, Queue, Iterator
  • 617. Merge Two Binary Trees - Easy - TreeYou are given two binary trees root1 and root2. Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped w... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 111. Minimum Depth of Binary Tree - Easy - TreeGiven a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest l... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 96. Unique Binary Search Trees - Medium - TreeGiven an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.... Topics: Math, Dynamic Programming, Tree, Binary Search Tree, Binary Tree
  • 1285. Balance a Binary Search Tree - Medium - TreeGiven the root of a binary search tree, return a balanced binary search tree with the same node values. If there is more than one answer, return any o... Topics: Divide and Conquer, Greedy, Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 893. All Nodes Distance K in Binary Tree - Medium - TreeGiven the root of a binary tree, the value of a target node target, and an integer k, return an array of the values of all nodes that have a distance ... Topics: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 116. Populating Next Right Pointers in Each Node - Medium - TreeYou are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following defin... Topics: Linked List, Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 99. Recover Binary Search Tree - Medium - TreeYou are given the root of a binary search tree (BST), where the values of exactly two nodes of the tree were swapped by mistake. Recover the tree with... Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 783. Search in a Binary Search Tree - Easy - TreeYou are given the root of a binary search tree (BST) and an integer val. Find the node in the BST that the node's value equals val and return the subt... Topics: Tree, Binary Search Tree, Binary Tree
  • 107. Binary Tree Level Order Traversal II - Medium - TreeGiven the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf ... Topics: Tree, Breadth-First Search, Binary Tree
  • 975. Range Sum of BST - Easy - TreeGiven the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [... Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 530. Minimum Absolute Difference in BST - Easy - TreeGiven the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree
  • 173. Binary Search Tree Iterator - Medium - TreeImplement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST): BSTIterator(TreeNode root) Init... Topics: Stack, Tree, Design, Binary Search Tree, Binary Tree, Iterator
  • 513. Find Bottom Left Tree Value - Medium - TreeGiven the root of a binary tree, return the leftmost value in the last row of the tree.... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 764. N-ary Tree Level Order Traversal - Medium - TreeGiven an n-ary tree, return the level order traversal of its nodes' values. Nary-Tree input serialization is represented in their level order traversa... Topics: Tree, Breadth-First Search
  • 1544. Count Good Nodes in Binary Tree - Medium - TreeGiven a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X. Return the ... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 1035. Cousins in Binary Tree - Easy - TreeGiven the root of a binary tree with unique values and the values of two different nodes of the tree x and y, return true if the nodes corresponding t... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 109. Convert Sorted List to Binary Search Tree - Medium - TreeGiven the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced binary search tree.... Topics: Linked List, Divide and Conquer, Tree, Binary Search Tree, Binary Tree
  • 789. Kth Largest Element in a Stream - Easy - TreeYou are part of a university admissions office and need to keep track of the kth highest test score from applicants in real-time. This helps to determ... Topics: Tree, Design, Binary Search Tree, Heap (Priority Queue), Binary Tree, Data Stream
  • 95. Unique Binary Search Trees II - Medium - TreeGiven an integer n, return all the structurally unique BST's (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the... Topics: Dynamic Programming, Backtracking, Tree, Binary Search Tree, Binary Tree
  • 1021. Distribute Coins in Binary Tree - Medium - TreeYou are given the root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole t... Topics: Tree, Depth-First Search, Binary Tree
  • 2558. Minimum Number of Operations to Sort a Binary Tree by Level - Medium - TreeYou are given the root of a binary tree with unique values. In one operation, you can choose any two nodes at the same level and swap their values. Re... Topics: Tree, Breadth-First Search, Binary Tree
  • 655. Print Binary Tree - Medium - TreeGiven the root of a binary tree, construct a 0-indexed m x n string matrix res that represents a formatted layout of the tree. The formatted layout ma... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 1050. Construct Binary Search Tree from Preorder Traversal - Medium - TreeGiven an array of integers preorder, which represents the preorder traversal of a BST (i.e., binary search tree), construct the tree and return its ro... Topics: Array, Stack, Tree, Binary Search Tree, Monotonic Stack, Binary Tree
  • 2567. Closest Nodes Queries in a Binary Search Tree - Medium - TreeYou are given the root of a binary search tree and an array queries of size n consisting of positive integers. Find a 2D array answer of size n where ... Topics: Array, Binary Search, Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 1079. Sum of Root To Leaf Binary Numbers - Easy - TreeYou are given the root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most s... Topics: Tree, Depth-First Search, Binary Tree
  • 337. House Robber III - Medium - TreeThe thief has found himself a new place for his thievery again. There is only one entrance to this area, called root. Besides the root, each house has... Topics: Dynamic Programming, Tree, Depth-First Search, Binary Tree
  • 2364. Longest Path With Different Adjacent Characters - Hard - TreeYou are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree... Topics: Array, String, Tree, Depth-First Search, Graph, Topological Sort
  • 1492. Time Needed to Inform All Employees - Medium - TreeA company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company is the one with headID. Each employee has one di... Topics: Tree, Depth-First Search, Breadth-First Search
  • 112. Path Sum - Easy - TreeGiven the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 690. Employee Importance - Medium - TreeYou have a data structure of employee information, including the employee's unique ID, importance value, and direct subordinates' IDs. You are given a... Topics: Array, Hash Table, Tree, Depth-First Search, Breadth-First Search
  • 988. Flip Equivalent Binary Trees - Medium - TreeFor a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. A binary tree X is flip e... Topics: Tree, Depth-First Search, Binary Tree
  • 1275. Validate Binary Tree Nodes - Medium - TreeYou have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only if all the... Topics: Tree, Depth-First Search, Breadth-First Search, Union Find, Graph, Binary Tree
  • 662. Maximum Width of Binary Tree - Medium - TreeGiven the root of a binary tree, return the maximum width of the given tree. The maximum width of a tree is the maximum width among all levels. The wi... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 2627. Difference Between Maximum and Minimum Price Sum - Hard - TreeThere exists an undirected and initially unrooted tree with n nodes indexed from 0 to n - 1. You are given the integer n and a 2D integer array edges ... Topics: Array, Dynamic Programming, Tree, Depth-First Search
  • 998. Check Completeness of a Binary Tree - Medium - TreeGiven the root of a binary tree, determine if it is a complete binary tree. In a complete binary tree, every level, except possibly the last, is compl... Topics: Tree, Breadth-First Search, Binary Tree
  • 449. Serialize and Deserialize BST - Medium - TreeSerialization is converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted acr... Topics: String, Tree, Depth-First Search, Breadth-First Search, Design, Binary Search Tree, Binary Tree
  • 1254. Deepest Leaves Sum - Medium - TreeGiven the root of a binary tree, return the sum of values of its deepest leaves.... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 2461. Amount of Time for Binary Tree to Be Infected - Medium - TreeYou are given the root of a binary tree with unique values, and an integer start. At minute 0, an infection starts from the node with value start. Eac... Topics: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 1114. Binary Search Tree to Greater Sum Tree - Medium - TreeGiven the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus... Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 257. Binary Tree Paths - Easy - TreeGiven the root of a binary tree, return all root-to-leaf paths in any order. A leaf is a node with no children.... Topics: String, Backtracking, Tree, Depth-First Search, Binary Tree
  • 1493. Frog Position After T Seconds - Hard - TreeGiven an undirected tree consisting of n vertices numbered from 1 to n. A frog starts jumping from vertex 1. In one second, the frog jumps from its cu... Topics: Tree, Depth-First Search, Breadth-First Search, Graph
  • 117. Populating Next Right Pointers in Each Node II - Medium - TreeGiven a binary tree struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there... Topics: Linked List, Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 404. Sum of Left Leaves - Easy - TreeGiven the root of a binary tree, return the sum of all left leaves. A leaf is a node with no children. A left leaf is a leaf that is the left child of... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 637. Average of Levels in Binary Tree - Easy - TreeGiven the root of a binary tree, return the average value of the nodes on each level in the form of an array. Answers within 10-5 of the actual answer... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 925. Construct Binary Tree from Preorder and Postorder Traversal - Medium - TreeGiven two integer arrays, preorder and postorder where preorder is the preorder traversal of a binary tree of distinct values and postorder is the pos... Topics: Array, Hash Table, Divide and Conquer, Tree, Binary Tree
  • 776. N-ary Tree Postorder Traversal - Easy - TreeGiven the root of an n-ary tree, return the postorder traversal of its nodes' values. Nary-Tree input serialization is represented in their level orde... Topics: Stack, Tree, Depth-First Search
  • 2646. Kth Largest Sum in a Binary Tree - Medium - TreeYou are given the root of a binary tree and a positive integer k. The level sum in the tree is the sum of the values of the nodes that are on the same... Topics: Tree, Breadth-First Search, Sorting, Binary Tree
  • 775. N-ary Tree Preorder Traversal - Easy - TreeGiven the root of an n-ary tree, return the preorder traversal of its nodes' values. Nary-Tree input serialization is represented in their level order... Topics: Stack, Tree, Depth-First Search
  • 113. Path Sum II - Medium - TreeGiven the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum.... Topics: Backtracking, Tree, Depth-First Search, Binary Tree
  • 1092. Maximum Difference Between Node and Ancestor - Medium - TreeGiven the root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor... Topics: Tree, Depth-First Search, Binary Tree
  • 2060. Merge BSTs to Create Single BST - Hard - TreeYou are given n BST (binary search tree) root nodes for n separate BSTs stored in an array trees (0-indexed). Each BST in trees has at most 3 nodes, a... Topics: Hash Table, Binary Search, Tree, Depth-First Search, Binary Tree
  • 671. Second Minimum Node In a Binary Tree - Easy - TreeGiven a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. ... Topics: Tree, Depth-First Search, Binary Tree
  • 1116. Maximum Level Sum of a Binary Tree - Medium - TreeGiven the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. Return the smallest level x such that the sum ... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 1005. Univalued Binary Tree - Easy - TreeA binary tree is uni-valued if every node in the tree has the same value. Given the root of a binary tree, return true if the given tree is uni-valued... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 930. All Possible Full Binary Trees - Medium - TreeGiven an integer n, return a list of all possible full binary trees with n nodes. Each node of each tree in the answer must have Node.val == 0. Each e... Topics: Dynamic Programming, Tree, Recursion, Memoization, Binary Tree
  • 2306. Create Binary Tree From Descriptions - Medium - TreeYou are given a 2D integer array descriptions where descriptions[i] = [parenti, childi, isLefti] indicates that parenti is the parent of childi in a b... Topics: Array, Hash Table, Tree, Binary Tree
  • 437. Path Sum III - Medium - TreeGiven the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum. The ... Topics: Tree, Depth-First Search, Binary Tree
  • 2493. Reverse Odd Levels of Binary Tree - Medium - TreeGiven the root of a perfect binary tree, reverse the node values at each odd level of the tree. For example, suppose the node values at level 3 are [2... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 3854. Maximum Profit from Trading Stocks with Discounts - Hard - TreeYou are given an integer n, representing the number of employees in a company. Each employee is assigned a unique ID from 1 to n, and employee 1 is th... Topics: Array, Dynamic Programming, Tree, Depth-First Search
  • 100. Same Tree - Easy - TreeGiven the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they a... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 538. Convert BST to Greater Tree - Medium - TreeGiven the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus... Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 772. Construct Quad Tree - Medium - TreeGiven a n * n matrix grid of 0's and 1's only. We want to represent grid with a Quad-Tree. Return the root of the Quad-Tree representing grid. A Quad-... Topics: Array, Divide and Conquer, Tree, Matrix
  • 3486. Count the Number of Good Nodes - Medium - TreeThere is an undirected tree with n nodes labeled from 0 to n - 1, and rooted at node 0. You are given a 2D integer array edges of length n - 1, where ... Topics: Tree, Depth-First Search
  • 904. Leaf-Similar Trees - Easy - TreeConsider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence. For example, in the given t... Topics: Tree, Depth-First Search, Binary Tree
  • 1030. Smallest String Starting From Leaf - Medium - TreeYou are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'. Return the lexicographic... Topics: String, Backtracking, Tree, Depth-First Search, Binary Tree
  • 3909. Minimum Increments to Equalize Leaf Paths - Medium - TreeYou are given an integer n and an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1. This is represented by a 2D array edges of l... Topics: Array, Dynamic Programming, Tree, Depth-First Search
  • 1484. Linked List in Binary Tree - Medium - TreeGiven a binary tree root and a linked list with head as the first node. Return True if all the elements in the linked list starting from the head corr... Topics: Linked List, Tree, Depth-First Search, Binary Tree
  • 1207. Delete Nodes And Return Forest - Medium - TreeGiven the root of a binary tree, each node in the tree has a distinct value. After deleting all nodes with a value in to_delete, we are left with a fo... Topics: Array, Hash Table, Tree, Depth-First Search, Binary Tree
  • 2531. Create Components With Same Value - Hard - TreeThere is an undirected tree with n nodes labeled from 0 to n - 1. You are given a 0-indexed integer array nums of length n where nums[i] represents th... Topics: Array, Math, Tree, Depth-First Search, Enumeration
  • 2217. Step-By-Step Directions From a Binary Tree Node to Another - Medium - TreeYou are given the root of a binary tree with n nodes. Each node is uniquely assigned a value from 1 to n. You are also given an integer startValue rep... Topics: String, Tree, Depth-First Search, Binary Tree
  • 1427. All Elements in Two Binary Search Trees - Medium - TreeGiven two binary search trees root1 and root2, return a list containing all the integers from both trees sorted in ascending order.... Topics: Tree, Depth-First Search, Binary Search Tree, Sorting, Binary Tree
  • 1008. Binary Tree Cameras - Hard - TreeYou are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its imme... Topics: Dynamic Programming, Tree, Depth-First Search, Binary Tree
  • 3906. Kth Smallest Path XOR Sum - Hard - TreeYou are given an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1. Each node i has an integer value vals[i], and its parent is g... Topics: Array, Tree, Depth-First Search, Ordered Set
  • 654. Maximum Binary Tree - Medium - TreeYou are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm: Create... Topics: Array, Divide and Conquer, Stack, Tree, Monotonic Stack, Binary Tree
  • 669. Trim a Binary Search Tree - Medium - TreeGiven the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lies in [low, hig... Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 2416. Evaluate Boolean Binary Tree - Easy - TreeYou are given the root of a full binary tree with the following properties: Leaf nodes have either the value 0 or 1, where 0 represents False and 1 re... Topics: Tree, Depth-First Search, Binary Tree
  • 687. Longest Univalue Path - Medium - TreeGiven the root of a binary tree, return the length of the longest path, where each node in the path has the same value. This path may or may not pass ... Topics: Tree, Depth-First Search, Binary Tree
  • 863. Sum of Distances in Tree - Hard - TreeThere is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are given the integer n and the array edges where edge... Topics: Dynamic Programming, Tree, Depth-First Search, Graph
  • 3900. Find Weighted Median Node in Tree - Hard - TreeYou are given an integer n and an undirected, weighted tree rooted at node 0 with n nodes numbered from 0 to n - 1. This is represented by a 2D array ... Topics: Array, Binary Search, Dynamic Programming, Tree, Depth-First Search
  • 2677. Cousins in Binary Tree II - Medium - TreeGiven the root of a binary tree, replace the value of each node in the tree with the sum of all its cousins' values. Two nodes of a binary tree are co... Topics: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 2384. Root Equals Sum of Children - Easy - TreeYou are given the root of a binary tree that consists of exactly 3 nodes: the root, its left child, and its right child. Return true if the value of t... Topics: Tree, Binary Tree
  • 1731. Even Odd Tree - Medium - TreeA binary tree is named Even-Odd if it meets the following conditions: The root of the binary tree is at level index 0, its children are at level index... Topics: Tree, Breadth-First Search, Binary Tree
  • 2717. Collect Coins in a Tree - Hard - TreeThere exists an undirected and unrooted tree with n nodes indexed from 0 to n - 1. You are given an integer n and a 2D integer array edges of length n... Topics: Array, Tree, Graph, Topological Sort
  • 2131. Smallest Missing Genetic Value in Each Subtree - Hard - TreeThere is a family tree rooted at 0 consisting of n nodes numbered 0 to n - 1. You are given a 0-indexed integer array parents, where parents[i] is the... Topics: Dynamic Programming, Tree, Depth-First Search, Union Find
  • 331. Verify Preorder Serialization of a Binary Tree - Medium - TreeOne way to serialize a binary tree is to use preorder traversal. When we encounter a non-null node, we record the node's value. If it is a null node, ... Topics: String, Stack, Tree, Binary Tree
  • 501. Find Mode in Binary Search Tree - Easy - TreeGiven the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it. If the tree h... Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 1554. Minimum Time to Collect All Apples in a Tree - Medium - TreeGiven an undirected tree consisting of n vertices numbered from 0 to n-1, which has some apples in their vertices. You spend 1 second to walk over one... Topics: Hash Table, Tree, Depth-First Search, Breadth-First Search
  • 955. Complete Binary Tree Inserter - Medium - TreeA complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possibl... Topics: Tree, Breadth-First Search, Design, Binary Tree
  • 3875. Maximum Good Subtree Score - Hard - TreeYou are given an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1. Each node i has an integer value vals[i], and its parent is g... Topics: Array, Dynamic Programming, Bit Manipulation, Tree, Depth-First Search, Bitmask
  • 3645. Maximize the Number of Target Nodes After Connecting Trees II - Hard - TreeThere exist two undirected trees with n and m nodes, labeled from [0, n - 1] and [0, m - 1], respectively. You are given two 2D integer arrays edges1 ... Topics: Tree, Depth-First Search, Breadth-First Search
  • 2347. Count Nodes Equal to Average of Subtree - Medium - TreeGiven the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree. Note: Th... Topics: Tree, Depth-First Search, Binary Tree
  • 832. Binary Tree Pruning - Medium - TreeGiven the root of a binary tree, return the same tree where every subtree (of the given tree) not containing a 1 has been removed. A subtree of a node... Topics: Tree, Depth-First Search, Binary Tree
  • 896. Smallest Subtree with all the Deepest Nodes - Medium - TreeGiven the root of a binary tree, the depth of each node is the shortest distance to the root. Return the smallest subtree such that it contains all th... Topics: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 1218. Lowest Common Ancestor of Deepest Leaves - Medium - TreeGiven the root of a binary tree, return the lowest common ancestor of its deepest leaves. Recall that: The node of a binary tree is a leaf if and only... Topics: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 3842. Number of Ways to Assign Edge Weights II - Hard - TreeThere is an undirected tree with n nodes labeled from 1 to n, rooted at node 1. The tree is represented by a 2D integer array edges of length n - 1, w... Topics: Array, Math, Dynamic Programming, Tree, Depth-First Search
  • 563. Binary Tree Tilt - Easy - TreeGiven the root of a binary tree, return the sum of every tree node's tilt. The tilt of a tree node is the absolute difference between the sum of all l... Topics: Tree, Depth-First Search, Binary Tree
  • 515. Find Largest Value in Each Tree Row - Medium - TreeGiven the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 508. Most Frequent Subtree Sum - Medium - TreeGiven the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any orde... Topics: Hash Table, Tree, Depth-First Search, Binary Tree
  • 1692. Number of Ways to Reorder Array to Get Same BST - Hard - TreeGiven an array nums that represents a permutation of integers from 1 to n. We are going to construct a binary search tree (BST) by inserting the eleme... Topics: Array, Math, Divide and Conquer, Dynamic Programming, Tree, Union Find, Binary Search Tree, Memoization, Combinatorics, Binary Tree
  • 3844. Number of Ways to Assign Edge Weights I - Medium - TreeThere is an undirected tree with n nodes labeled from 1 to n, rooted at node 1. The tree is represented by a 2D integer array edges of length n - 1, w... Topics: Math, Tree, Depth-First Search
  • 1722. Throne Inheritance - Medium - TreeA kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while, someone in the family dies or a child is born. The ki... Topics: Hash Table, Tree, Depth-First Search, Design
  • 3687. Longest Special Path - Hard - TreeYou are given an undirected tree rooted at node 0 with n nodes numbered from 0 to n - 1, represented by a 2D array edges of length n - 1, where edges[... Topics: Array, Hash Table, Tree, Depth-First Search, Prefix Sum
  • 1243. Sum of Nodes with Even-Valued Grandparent - Medium - TreeGiven the root of a binary tree, return the sum of values of nodes with an even-valued grandparent. If there are no nodes with an even-valued grandpar... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 1296. Kth Ancestor of a Tree Node - Hard - TreeYou are given a tree with n nodes numbered from 0 to n - 1 in the form of a parent array parent where parent[i] is the parent of ith node. The root of... Topics: Binary Search, Dynamic Programming, Tree, Depth-First Search, Breadth-First Search, Design
  • 1740. Count Subtrees With Max Distance Between Cities - Hard - TreeThere are n cities numbered from 1 to n. You are given an array edges of size n-1, where edges[i] = [ui, vi] represents a bidirectional edge between c... Topics: Dynamic Programming, Bit Manipulation, Tree, Enumeration, Bitmask
  • 1450. Delete Leaves With a Given Value - Medium - TreeGiven a binary tree root and an integer target, delete all the leaf nodes with value target. Note that once you delete a leaf node with value target, ... Topics: Tree, Depth-First Search, Binary Tree
  • 2505. Number of Good Paths - Hard - TreeThere is a tree (i.e. a connected, undirected graph with no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges. You are gi... Topics: Array, Hash Table, Tree, Union Find, Graph, Sorting
  • 3829. Shortest Path in a Weighted Tree - Hard - TreeYou are given an integer n and an undirected, weighted tree rooted at node 1 with n nodes numbered from 1 to n. This is represented by a 2D array edge... Topics: Array, Tree, Depth-First Search, Binary Indexed Tree, Segment Tree
  • 1474. Longest ZigZag Path in a Binary Tree - Medium - TreeYou are given the root of a binary tree. A ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (rig... Topics: Dynamic Programming, Tree, Depth-First Search, Binary Tree
  • 799. Minimum Distance Between BST Nodes - Easy - TreeGiven the root of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree.... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree
  • 933. Increasing Order Search Tree - Easy - TreeGiven the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every no... Topics: Stack, Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 3509. K-th Largest Perfect Subtree Size in Binary Tree - Medium - TreeYou are given the root of a binary tree and an integer k. Return an integer denoting the size of the kth largest perfect binary subtree, or -1 if it d... Topics: Tree, Depth-First Search, Sorting, Binary Tree
  • 1475. Maximum Sum BST in Binary Tree - Hard - TreeGiven a binary tree root, return the maximum sum of all keys of any sub-tree which is also a Binary Search Tree (BST). Assume a BST is defined as foll... Topics: Dynamic Programming, Tree, Depth-First Search, Binary Search Tree, Binary Tree
  • 3079. Minimum Edge Weight Equilibrium Queries in a Tree - Hard - TreeThere is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edge... Topics: Array, Tree, Graph, Strongly Connected Component
  • 2652. Count Number of Possible Root Nodes - Hard - TreeAlice has an undirected tree with n nodes labeled from 0 to n - 1. The tree is represented as a 2D integer array edges of length n - 1 where edges[i] ... Topics: Array, Hash Table, Dynamic Programming, Tree, Depth-First Search
  • 3439. Find Minimum Diameter After Merging Two Trees - Hard - TreeThere exist two undirected trees with n and m nodes, numbered from 0 to n - 1 and from 0 to m - 1, respectively. You are given two 2D integer arrays e... Topics: Tree, Depth-First Search, Breadth-First Search, Graph
  • 2564. Most Profitable Path in a Tree - Medium - TreeThere is an undirected tree with n nodes labeled from 0 to n - 1, rooted at node 0. You are given a 2D integer array edges of length n - 1 where edges... Topics: Array, Tree, Depth-First Search, Breadth-First Search, Graph
  • 652. Find Duplicate Subtrees - Medium - TreeGiven the root of a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one ... Topics: Hash Table, Tree, Depth-First Search, Binary Tree
  • 2568. Minimum Fuel Cost to Report to the Capital - Medium - TreeThere is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of n cities numbered from 0 to n - 1 and exa... Topics: Tree, Depth-First Search, Breadth-First Search, Graph
  • 3112. Count Valid Paths in a Tree - Hard - TreeThere is an undirected tree with n nodes labeled from 1 to n. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i]... Topics: Math, Dynamic Programming, Tree, Depth-First Search, Number Theory
  • 1498. Find a Corresponding Node of a Binary Tree in a Clone of That Tree - Easy - TreeGiven two binary trees original and cloned and given a reference to a node target in the original tree. The cloned tree is a copy of the original tree... Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
  • 1093. Recover a Tree From Preorder Traversal - Hard - TreeWe run a preorder depth-first search (DFS) on the root of a binary tree. At each node in this traversal, we output D dashes (where D is the depth of t... Topics: String, Tree, Depth-First Search, Binary Tree
  • 774. Maximum Depth of N-ary Tree - Easy - TreeGiven a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest le... Topics: Tree, Depth-First Search, Breadth-First Search
  • 1653. Number of Good Leaf Nodes Pairs - Medium - TreeYou are given the root of a binary tree and an integer distance. A pair of two different leaf nodes of a binary tree is said to be good if the length ... Topics: Tree, Depth-First Search, Binary Tree
  • 1875. Tree of Coprimes - Hard - TreeThere is a tree (i.e., a connected, undirected graph that has no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges. Each ... Topics: Array, Math, Tree, Depth-First Search, Number Theory
  • 2400. Minimum Score After Removals on a Tree - Hard - TreeThere is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are given a 0-indexed integer array nums of length n w... Topics: Array, Bit Manipulation, Tree, Depth-First Search
  • 606. Construct String from Binary Tree - Medium - TreeGiven the root node of a binary tree, your task is to create a string representation of the tree following a specific set of formatting rules. The rep... Topics: String, Tree, Depth-First Search, Binary Tree
  • 2104. Operations on Tree - Medium - TreeYou are given a tree with n nodes numbered from 0 to n - 1 in the form of a parent array parent where parent[i] is the parent of the ith node. The roo... Topics: Array, Hash Table, Tree, Depth-First Search, Breadth-First Search, Design
  • 2905. Count Paths That Can Form a Palindrome in a Tree - Hard - TreeYou are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1. The tree... Topics: Dynamic Programming, Bit Manipulation, Tree, Depth-First Search, Bitmask
  • 2545. Height of Binary Tree After Subtree Removal Queries - Hard - TreeYou are given the root of a binary tree with n nodes. Each node is assigned a unique value from 1 to n. You are also given an array queries of size m.... Topics: Array, Tree, Depth-First Search, Breadth-First Search, Binary Tree

Related LeetCode Topics

Tree LeetCode Problems - Interview Coder