

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the closest leaf in a Binary Tree in C++
Suppose, one binary tree is given. It has leaf nodes at different levels. Another pointer is given, that is pointing to a node. We have to find the distance to the nearest leaf node from the pointed node. Consider the tree is like below −
Here leaf nodes are 2, -2 and 6. If the pointer is pointing to node -5, The nearest nodes from -5 will be at distance 1.
To solve this, we will traverse the subtree rooted with the given node, and find the closest leaf in the subtree, then store the distance. Now traversing tree starting from the root, if the node x is present in the left subtree, then search into the right subtree, and vice versa.
Example
#include<iostream> using namespace std; class Node { public: int data; Node *left, *right; }; Node* getNode(int data) { Node* node = new Node; node->data = data; node->left = node->right = NULL; return node; } void getLeafDownward(Node *root, int level, int *minDist) { if (root == NULL) return ; if (root->left == NULL && root->right == NULL) { if (level < (*minDist)) *minDist = level; return; } getLeafDownward(root->left, level+1, minDist); getLeafDownward(root->right, level+1, minDist); } int getFromParent(Node * root, Node *x, int *minDist) { if (root == NULL) return -1; if (root == x) return 0; int l = getFromParent(root->left, x, minDist); if (l != -1) { getLeafDownward(root->right, l+2, minDist); return l+1; } int r = getFromParent(root->right, x, minDist); if (r != -1) { getLeafDownward(root->left, r+2, minDist); return r+1; } return -1; } int minimumDistance(Node *root, Node *x) { int minDist = INT8_MAX; getLeafDownward(x, 0, &minDist); getFromParent(root, x, &minDist); return minDist; } int main() { Node* root = getNode(4); root->left = getNode(2); root->right = getNode(-5); root->right->left = getNode(-2); root->right->right = getNode(6); Node *x = root->right; cout << "Closest distance of leaf from " << x->data <<" is: " << minimumDistance(root, x); }
Output
Closest distance of leaf from -5 is: 1
- Related Questions & Answers
- Find the closest element in Binary Search Tree in C++
- Program to find leaf and non-leaf nodes of a binary tree in Python
- C++ Program to Find Deepest Left Leaf in a Binary Tree
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Find the sum of left leaf nodes of a given Binary Tree in C++
- Program to print the longest leaf to leaf path in a Binary tree using C++
- Count Non-Leaf nodes in a Binary Tree in C++
- Deepest left leaf node in a binary tree in C++
- Closest Binary Search Tree Value II in C++
- Find height of a special binary tree whose leaf nodes are connected in C++
- Product of all leaf nodes of binary tree in C++
- Print the first shortest root to leaf path in a Binary Tree in C++ Programming.
- Find the Deepest Node in a Binary Tree in C++
- Program to find sum of longest sum path from root to leaf of a binary tree in Python
- Program to print the first shortest root to leaf path in a Binary Tree using C++
Advertisements