I need to describe an algorithm relying only on the Binary Tree operations, that counts the number of leaves in a binary tree that their parents are the right children of a node. I need to also figure out the pseudo code.
I have read examples, but most are about counting all leaves of a binary tree. I also somewhat understand the binary tree, and from what I can gather need to use inOrder to traverse the tree; however, I'm not sure what to do after that.
I have thought about doing an algorithm something like this:
So for every node we keep track of who (1) his parents and (2) grandparent are.
If node is leaf, then we check whether grandparent. right = parent.node
If yes, then we increment the count; else nothing
After we finished with a node (regardless if it is a leaf or not) => proceed with the next one (e.g. child). Then the analyzed node becomes parent and former parent becomes grandparent. And so on (we can regard this as a recursive procedure while traversing the nodes stored in a vector).
I'm just not sure if I'm way off point with this. Any help is extremely appreciated! Thank you.