how would i do this? I am not sure when I would stop the bst search.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If each node of your tree has a field
numLeftthat tells you how many nodes there are in its left subtree (counting itself too), then you can do this inO(log N)Just keep adding
numLeftto a global result variable for each node whose value is less thanx:This will only count the numbers. If you want to print them, you need to write a depth first traversal that will print a subtree given as parameter. You can find plenty of those online, so I won’t post that.