A product catalog keeps prices in a binary search tree: every node's left subtree holds smaller prices and its right subtree holds bigger ones. Prices are unique.
Write findPrice(root, price) that returns the node whose value is price, together with everything below it (return the node itself). If the price isn't in the tree, return null. The examples show the returned subtree in level order, and [] for null.
Input: root = [40,20,70,10,30], price = 20 Output: [20,10,30]
20 < 40, so go left and find 20, with its children 10 and 30.
Input: root = [40,20,70,10,30], price = 25 Output: []
25 would be under 30, but 30 has no left child, so it isn't there.
0 <= number of prices <= 100001 <= price <= 10^7Trees are written level by level, like [1, 2, 3, null, 4], where null is a missing child. Your function works with TreeNode objects, each with val, left, and right.
Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #335 Find a Price in the Catalog Tree. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.