Need to get the leaf node that has minimum depth. I cannot think of a good way to do it without storing additional information in each node, please suggest, thanks very much.
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.
The brute force solution is a breadth-first search terminating at the first leaf found, this will be easier to implement iteratively than recursively.
See for instance the pseudo-code in my answer to “Breadth First Vs Depth First” just add another condition to the while-loop.
BTW–This will get you a leaf with the minimum depth, as there may be more than one at that depth. Getting the full set of minimum depth leaves is a little harder. I guess go with an iterative deepening strategy.
Finding out what level that node is one.
Three choices:
Find the node first and the search down the tree for it. It sounds wasteful, but that second search requires visiting only as many nodes as the level, so it really is fast.
Alternately you can keep track as you go. You use three counters
levelCounter,thisLevelCounterandnextLevelCounter. Every time you more to a new node you decrementthisLevelCounter, and when it hits zero you’ve moved down a level so doEvery time you add a child node to the search list, increment
nextLevelCounter.Every time you store a new child node increment
nextLevelCounterFinally, the iterative deepening strategy gives you the sucess level for free (which iteration finds it…) and has the same order of performance (though a slightly higher multiplier) as the breadth first search.