Skip to content
Optimal Lab

Practice · 13 problems

Trees

0 of 13 solved on this device

Watch it first · interactiveTrees: let the children answer first →
  1. 024Invert Binary TreeGiven the root of a binary tree, invert it — swap every node's left and right children — and return the root. A node is { val, left, right }.Easy
  2. 025Maximum Depth of Binary TreeGiven the root of a binary tree, return its maximum depth: the number of nodes along the longest path from the root down to a leaf.Easy
  3. 026Validate Binary Search TreeGiven the root of a binary tree, determine whether it is a valid binary search tree: every node in a left subtree is strictly less than the node, every node in a right subtree is strictly greater, and both subtrees are themselves valid BSTs.Medium
  4. 027Lowest Common Ancestor of a BSTGiven a binary search tree and two nodes `p` and `q` present in it, return their lowest common ancestor. A node may be a descendant of itself.Medium
  5. 028Binary Tree Level Order TraversalGiven the root of a binary tree, return its level-order traversal: the node values grouped level by level, from left to right.Medium
  6. 069Same TreeGiven the roots of two binary trees, return true if they are structurally identical and every corresponding node holds the same value.Easy
  7. 070Balanced Binary TreeGiven the root of a binary tree, determine whether it is height-balanced: every node's two subtrees differ in height by at most 1.Easy
  8. 071Diameter of Binary TreeGiven the root of a binary tree, return its diameter: the length of the longest path between any two nodes, measured in EDGES. The path need not pass through the root.Easy
  9. 072Binary Tree Maximum Path SumA path is any sequence of nodes connected by edges, appearing at most once each, and need not pass through the root. Return the maximum sum of node values along any path.Hard
  10. 113Kth Smallest Element in a BSTGiven the root of a binary search tree and an integer `k`, return the kth smallest value in the tree (1-indexed).Medium
  11. 114Binary Tree Right Side ViewGiven the root of a binary tree, imagine standing to its right. Return the values of the nodes visible from top to bottom.Medium
  12. 115Construct Binary Tree from Preorder and Inorder TraversalGiven two arrays `preorder` and `inorder` representing the traversals of the same binary tree with unique values, reconstruct and return the tree.Medium
  13. 116Serialize and Deserialize Binary TreeDesign an algorithm to serialize a binary tree to a string and deserialize that string back into the identical tree. There is no restriction on the format you choose.Hard