5 stars based on
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:.
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.