## Interview Questions

5 stars based on
70 reviews

Join Stack Overflow to learn, share knowledge, and build your career. I am trying to write a method that will return true if a binary tree is full each node has 2 child nodes or none and false otherwise. This is working some of the time but not all. Any suggestions about where I am going wrong?

Also, if the root node is null, then your tree is full. So the last return should be return true. The problem is the else if and lack of return statements. Also no need to checking for null so much, and use of a method makes it more readable. All the algoritms above return true in this case as they shouldn't: So, hope this helps:.

You are not fully traversing the tree. Use recursion to hit all the nodes. Check the root node. If there are no children, return true. If there are children, make sure there are two, and then check each of them recursively. By posting your check if a binary search tree is full, you agree to the privacy policy and terms of service. Email Sign Up or sign in with Google. Check if a binary tree is full in Java? Aleksandr Podkutin 1, 1 12 Can you say your problem clearly? If the tree is complete, I will always get true.

However, if it is incomplete sometimes I get true which is wrong and sometimes false. The first if condition is nor correct. So where is the leaf test? It's a good point.

Better to put it as a comment on my answer though so someone looking at my answer would spot that. Try to add return to each statement. JFPicard 3, 2 11 So, if the left is full, don't even look at the right? Your right, I've updated my anwser.

So, hope this helps: All the algoritms [sic] above return true in this case - which this case? Given that the question explicates binary tree is full each node has 2 child nodes or none a commendable diligencethis tries to answer a different question. Click the link 'complete binary tree' so you can see which case.

If you test those algoritms, they will return true, they shouldn't do that cause is not a full tree but a complete tree. I think that the if statements should check if a binary search tree is full as follows: Tree is only full if both sub trees are full return isFull root.

Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Stack Overflow works best with **Check if a binary search tree is full** enabled.