What are leaf nodes?
Leaf nodes are those nodes in a tree which have no left and no right child. our problem is to find out all the leaf nodes in a binary tree and store them in a vector.
![]() |
binary tree |
In this figure , the leaf nodes are :
Suppose we have :
Form a new node :
Or
After applying it to tree , we will check it the node is a leaf node.
To check whether this node is leaf node or not :
If it return true , it means `node` is a leaf node else it will other `node`.
How to find all leaf nodes in a binary tree?
Here we will find all the leaf nodes in a binary tree and store these in a vector.
Algorithm :
Use level order traversal to traverse all the nodes in a binary tree.
For every node check the above condition if the node is leaf node or not.
If it is not a leaf node ,then move forward else push it in the vector.
Code :
References :
https://mycodevillage.blogspot.com/2022/09/429-n-ary-tree-level-order-traversal.html
Comments
Post a Comment