Print all leaf nodes of a Binary Tree from left to right | C ++ program to print leaf nodes from left to right ; A Binary Tree Node ; function to print leaf nodes from left to right ; if node is null , return ; if node is leaf node , print its data ; if left child exists , check for leaf recursively ; if right child exists , check for leaf recursively ; Utility function to create a new tree node ; Driver program to test above functions ; Let us create binary tree shown in above diagram ; print leaf nodes of the given tree
#include <iostream> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; void printLeafNodes ( Node * root ) { if ( ! root ) return ; if ( ! root -> left && ! root -> right ) { cout << root -> data << " ▁ " ; return ; } if ( root -> left ) printLeafNodes ( root -> left ) ; if ( root -> right ) printLeafNodes ( root -> right ) ; } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> right -> left = newNode ( 5 ) ; root -> right -> right = newNode ( 8 ) ; root -> right -> left -> left = newNode ( 6 ) ; root -> right -> left -> right = newNode ( 7 ) ; root -> right -> right -> left = newNode ( 9 ) ; root -> right -> right -> right = newNode ( 10 ) ; printLeafNodes ( root ) ; return 0 ; }