C++ Program to Find Deepest Left Leaf in a Binary Tree

This C++ Program finds the Deepest Left Leaf in a Binary Tree.

Here is source code of the C++ Program to Find the Deepest Left Leaf in a Binary Tree. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C++ program to Find Deepest Left Leaf in a Binary Tree
  3.  */
  4. #include <cstdio>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. /* 
  9.  * Node Declaration
  10.  */ 
  11. struct Node
  12. {
  13.     int val;
  14.     Node *left, *right;
  15. };
  16. /* 
  17.  * create new node
  18.  */ 
  19. Node *newNode(int data)
  20. {
  21.     Node *temp = new Node;
  22.     temp->val = data;
  23.     temp->left = temp->right =  NULL;
  24.     return temp;
  25. }
  26.  
  27. /* 
  28.  * find deepest leaf node
  29.  */ 
  30. void deepestLeftLeafUtil(Node *root, int lvl, int *maxlvl, bool isLeft, Node **resPtr)
  31. {
  32.     if (root == NULL)
  33.         return;
  34.     if (isLeft && !root->left && !root->right && lvl > *maxlvl)
  35.     {
  36.         *resPtr = root;
  37.         *maxlvl = lvl;
  38.         return;
  39.     }
  40.     deepestLeftLeafUtil(root->left, lvl + 1, maxlvl, true, resPtr);
  41.     deepestLeftLeafUtil(root->right, lvl + 1, maxlvl, false, resPtr);
  42. }
  43. /* 
  44.  * implement deepestLeftLeafUtil.
  45.  */  
  46. Node* deepestLeftLeaf(Node *root)
  47. {
  48.     int maxlevel = 0;
  49.     Node *result = NULL;
  50.     deepestLeftLeafUtil(root, 0, &maxlevel, false, &result);
  51.     return result;
  52. }
  53.  
  54. /* 
  55.  * Main
  56.  */  
  57. int main()
  58. {
  59.     Node* root = newNode(1);
  60.     root->left = newNode(2);
  61.     root->right = newNode(3);
  62.     root->left->left = newNode(4);
  63.     root->right->left = newNode(5);
  64.     root->right->right = newNode(6);
  65.     root->right->left->right = newNode(7);
  66.     root->right->right->right = newNode(8);
  67.     root->right->left->right->left = newNode(9);
  68.     root->right->right->right->right = newNode(10);
  69.     Node *result = deepestLeftLeaf(root);
  70.     if (result)
  71.         cout << "The deepest left child is " << result->val;
  72.     else
  73.         cout << "There is no left leaf in the given tree";
  74.     return 0;
  75. }

$ g++ deepest_leftleaf.cpp
$ a.out
 
The deepest left child is 9
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.