84Compute the diameter of a binary tree (longest path between any two nodes).▼mediumMetaGoogleAmazon2 replies◆ premiumThe diameter is not the tree's height, and the path need not pass through the root. The clean solution computes height and best path in one DFS, updating a global max at every node. Here is the answer and the subtlety candidates miss.Open full answer →
85Reconstruct a binary tree from its preorder and inorder traversals.▼mediumAmazonMicrosoftGoogle1 replies◆ premiumPreorder names the root; inorder splits the rest into left and right subtrees. Doing it naively is O(n squared); the strong answer uses a value-to-index map and a moving preorder pointer for O(n). Here is the answer and why both orders are required.Open full answer →
86Find the maximum path sum in a binary tree, where a path may start and end anywhere.▼hardMetaGoogleAmazon1 replies◆ premiumNegative subtrees, a path that bends through any node, and a return value that differs from the answer you track: this problem packs three traps into one DFS. Here is the clean O(n) solution and the reasoning that survives follow-ups.Open full answer →