Lexicographically smallest Palindromic Path in a Binary Tree

Binary trees are fundamental data structures in computer science, providing an efficient way to organize data hierarchically. When traversing these trees, we often uncover intriguing computational problems. Among these, identifying the lexicographically smallest palindromic path is a fascinating challenge. This article elucidates an effective C++ algorithm to solve this problem and provides a detailed example for better understanding.

Problem Statement

In a binary tree where each node represents a lowercase English letter, our objective is to discover the lexicographically smallest palindromic path. If several paths qualify, we can return any of them. If no palindromic path exists, we should return an empty string.


Our approach to this problem involves using a depth-first search (DFS) technique to traverse the binary tree. The DFS method allows us to explore each path from the root node to the leaf node.

C++ Solution

Here is the C++ code that implements the approach mentioned above −


using namespace std;

struct Node {
   char data;
   Node *left, *right;
   Node(char c) : data(c), left(NULL), right(NULL) {}

string smallestPalindrome(Node* node, string s) {
   if(node == NULL)
      return "";
   s += node->data;
   if(node->left == NULL && node->right == NULL)
      return string(s.rbegin(), s.rend()) == s ? s : "";
   string left = smallestPalindrome(node->left, s);
   string right = smallestPalindrome(node->right, s);
   if(left == "")
      return right;
   if(right == "")
      return left;
   return min(left, right);

string smallestPalindromicPath(Node* root) {
   return smallestPalindrome(root, "");

int main() {
   Node* root = new Node('a');
   root->left = new Node('b');
   root->right = new Node('a');
   root->left->left = new Node('a');
   root->left->right = new Node('a');
   root->right->left = new Node('b');
   root->right->right = new Node('a');
   cout << smallestPalindromicPath(root) << endl;
   return 0;



Test Case and Explanation

Let's examine a binary tree with the following structure −

   /   \
  b     a
 / \   / \
a   a b   a

In this binary tree, multiple paths from the root node to the leaf nodes exist. Among all these paths, the function will return the lexicographically smallest palindromic path. In this case, the possible palindromic paths are "aaa" and "aba". Thus, the output will be "aaa", which is the lexicographically smallest palindromic path.


Determining the lexicographically smallest palindromic path in a binary tree is an intriguing problem that combines tree traversal and string manipulation concepts. The C++ solution provided above employs a depth-first search approach to solve this problem effectively. Understanding such problems enhances your comprehension of binary trees and strengthens your problem-solving skills in computer science.

Updated on: 18-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started