Traverse the tree left-to-right. There are several critical points at which you'll want to store or compare information: When you hit the first leaf node When you hit subsequent leaf nodes When you hit the first leaf node with a different distance from the root Since this is homework, you should probably take it the rest of the way from that hint.
Traverse the tree left-to-right. There are several critical points at which you'll want to store or compare information: When you hit the first leaf node. When you hit subsequent leaf nodes.
When you hit the first leaf node with a different distance from the root. Since this is homework, you should probably take it the rest of the way from that hint.
So I would traverse the tree in preorder and find the first leaf node with if (tree. Left == null && tree. Right == null) and in the condition I would store the current depth to a global variable and then compare that with the next leaf node somehow and if they're equal return true.
If they're different I'd store the bigger value at maxValue and store the smaller value at minValue. Then if maxValue and minValue only differ by 1 or 0 by the end of it, is that then a complete binary tree? That's all I could come up with... – Jigglypuff May 6 at 16:40 You're on the right track.
However, you have to guarantee that the last row is filled left-to-right. So what must happen at the first leaf node after the left-most that has a different depth? What must not happen at leaf nodes subsequent to that step?
– Andy Thomas-Cramer May 6 at 16:48 Also, a better approach than a global variable is to carry data along the traversal. For example, you can pass the same object to the visitation of each node, and record observations in that object. Optionally, this object can also contain the behavior that should be performed on each node -- this is known as the Visitor pattern.
– Andy Thomas-Cramer May 6 at 16:53 Brain is fizzling out due to all-nighter. I'll try this out after I wake up tomorrow and report back. Thanks for the replies.
– Jigglypuff May 6 at 16:56 I was able to come up with something that works. Thank you. – Jigglypuff May 67 at 0:30.
You should use recursion: boolean check() { if (this.hasNoChild()) { return true; } else if (this.hasTwoChildren()) { return this.left.check() && this.right.check(); } return false; } Edit I forgot this: ... all nodes are as far left as possible This might be better: int sum() { // number of children, grandchildren, etc if (this.hasNoChild()) { return 0; } if (this.hasTwoChildren()) { return 2 + this.left.sum() + this.right.sum(); } return 1; // bad } boolean check() { if (this.hasNoChild()) { return true; } if (this.hasTwoChildren()) { int leftSum = left.sum() int rightSum = right.sum() return left.check() && right.check() && (leftSum >= rightSum) && (leftSum%2 == 0) && (rightSum%2 == 0); } return false; }.
2 Similarly to another (now deleted) answer, this is incorrect. For one thing, it's symmetric between left and right, whereas the definition requires that "all nodes are as far left as possible". – aix May 6 at 16:00 For another thing, it does not test that "every level, except possibly the last, is completely filled." – Andy Thomas-Cramer May 6 at 16:46.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.