C++ program to find the deepest node in a binary tree














































C++ program to find the deepest node in a binary tree



#include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node *left, *right; }; Node *newNode(int data) { Node *temp = new Node; temp->data = data; temp->left = temp->right = NULL; return temp; } void finddeepnode(Node *root, int level, int &maxLevel, int &res) { if (root != NULL) { find(root->left, ++level, maxLevel, res); if (level > maxLevel) { res = root->data; maxLevel = level; } finddeepnode(root->right, level, maxLevel, res); } } int deepestNode(Node *root) { int res = -1; int maxLevel = -1; finddeepnode(root, 0, maxLevel, res); return res; } int main() { Node* root = newNode(10); root->left = newNode(12); root->right = newNode(31); root->left->left = newNode(42); root->right->left = newNode(54); root->right->right = newNode(60); root->right->left->right = newNode(17); root->right->right->right = newNode(18); root->right->left->right->left = newNode(69); cout << deepestNode(root); return 0;
}


Comments