Open In App

Print the longest leaf to leaf path in a Binary tree

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

C++




// C++ program to print the longest leaf to leaf
// path
#include <bits/stdc++.h>
using namespace std;
 
// Tree node structure used in the program
struct Node {
    int data;
    Node *left, *right;
};
 
struct Node* newNode(int data)
{
    struct Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
 
    return (node);
}
 
// Function to find height of a tree
int height(Node* root, int& ans, Node*(&k), int& lh, int& rh,
                                                     int& f)
{
    if (root == NULL)
        return 0;
 
    int left_height = height(root->left, ans, k, lh, rh, f);
 
    int right_height = height(root->right, ans, k, lh, rh, f);
 
    // update the answer, because diameter of a
    // tree is nothing but maximum value of
    // (left_height + right_height + 1) for each node
 
    if (ans < 1 + left_height + right_height) {
 
        ans = 1 + left_height + right_height;
 
        // save the root, this will help us finding the
        //  left and the right part of the diameter
        k = root;
 
        // save the height of left & right subtree as well.
        lh = left_height;
        rh = right_height;
    }
 
    return 1 + max(left_height, right_height);
}
 
// prints the root to leaf path
void printArray(int ints[], int len, int f)
{
    int i;
     
   
 
    // print left part of the path in reverse order
    if (f == 0) {
        for (i = len - 1; i >= 0; i--) {
            printf("%d ", ints[i]);
        }
    }
 
    // print right part of the path
    else if (f == 1) {
        for (i = 0; i < len; i++) {
            printf("%d ", ints[i]);
        }
    }
}
 
// this function finds out all the root to leaf paths
void printPathsRecur(Node* node, int path[], int pathLen,
                                         int max, int& f)
{
    if (node == NULL)
        return;
 
    // append this node to the path array
    path[pathLen] = node->data;
    pathLen++;
 
    // If it's a leaf, so print the path that led to here
    if (node->left == NULL && node->right == NULL) {
 
        // print only one path which is equal to the
        // height of the tree.
        if (pathLen == max && (f == 0 || f == 1)) {
            printArray(path, pathLen, f);
            f = 2;
        }
    }
 
    else {
 
        // otherwise try both subtrees
        printPathsRecur(node->left, path, pathLen, max, f);
        printPathsRecur(node->right, path, pathLen, max, f);
    }
}
 
// Computes the diameter of a binary tree with given root.
void diameter(Node* root)
{
    if (root == NULL)
        return;
 
    // lh will store height of left subtree
    // rh will store height of right subtree
    int ans = INT_MIN, lh = 0, rh = 0;
 
    // f is a flag whose value helps in printing
    // left & right part of the diameter only once
    int f = 0;
    Node* k;
     
  
    int height_of_tree = height(root, ans, k, lh, rh, f);
     
   
    int lPath[100], pathlen = 0;
 
    // print the left part of the diameter
    printPathsRecur(k->left, lPath, pathlen, lh, f);
    printf("%d ", k->data);
    int rPath[100];
    f = 1;
 
    // print the right part of the diameter
    printPathsRecur(k->right, rPath, pathlen, rh, f);
}
 
// Driver code
int main()
{
    // Enter the binary tree ...
    //           1
    //         /   \    
    //        2     3
    //      /   \  
    //     4     5
    //      \   / \
    //       8 6   7
    //      /
    //     9
    struct Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->left->right->left = newNode(6);
    root->left->right->right = newNode(7);
    root->left->left->right = newNode(8);
    root->left->left->right->left = newNode(9);
 
    diameter(root);
 
    return 0;
}


Java




// Java program to print the longest leaf to leaf
// path
import java.io.*;
 
// Tree node structure used in the program
class Node
{
    int data;
    Node left, right;
    Node(int val)
    {
        data = val;
        left = right = null;
    }
}
class GFG
{
    static int ans, lh, rh, f;
    static Node k;
    public static Node Root;
   
    // Function to find height of a tree
    static int height(Node root)
    {
        if (root == null)
            return 0;
        int left_height = height(root.left);
        int right_height = height(root.right);
       
        // update the answer, because diameter of a
        // tree is nothing but maximum value of
        // (left_height + right_height + 1) for each node
        if (ans < 1 + left_height + right_height)
        {
            ans = 1 + left_height + right_height;
  
            // save the root, this will help us finding the
            //  left and the right part of the diameter
            k = root;
  
            // save the height of left & right subtree as well.
            lh = left_height;
            rh = right_height;
        }
        return 1 + Math.max(left_height, right_height);
 
    }
   
    // prints the root to leaf path
    static void printArray(int[] ints, int len)
    {
        int i;
    
        // print left part of the path in reverse order
        if(f == 0)
        {
            for(i = len - 1; i >= 0; i--)
            {
                System.out.print(ints[i] + " ");
            }
        }
        else if(f == 1)
        {
            for (i = 0; i < len; i++)
            {
                System.out.print(ints[i] + " ");  
            }
        }
    }
     
    // this function finds out all the root to leaf paths
    static void printPathsRecur(Node node, int[] path,
                                int pathLen, int max)
    {
        if (node == null)
            return;
       
        // append this node to the path array
        path[pathLen] = node.data;
        pathLen++;
         
        // If it's a leaf, so print the path that led to here
        if (node.left == null && node.right == null)
        {
           
            // print only one path which is equal to the
            // height of the tree.
            if (pathLen == max && (f == 0 || f == 1))
            {
                printArray(path, pathLen);
                f = 2;
            }
        }
        else
        {
           
            // otherwise try both subtrees
        printPathsRecur(node.left, path, pathLen, max);
        printPathsRecur(node.right, path, pathLen, max);
        }
    }
     
    // Computes the diameter of a binary tree with given root.
    static void diameter(Node root)
    {
        if (root == null)
            return;
       
        // lh will store height of left subtree
        // rh will store height of right subtree
        ans = Integer.MIN_VALUE;
        lh = 0;
        rh = 0;
       
        // f is a flag whose value helps in printing
        // left & right part of the diameter only once
        f = 0;
        int height_of_tree = height(root);
         
        int[] lPath = new int[100];
        int pathlen = 0;
       
        // print the left part of the diameter
        printPathsRecur(k.left, lPath, pathlen, lh);
        System.out.print(k.data+" ");
        int[] rPath = new int[100];
        f = 1;
       
        // print the right part of the diameter
        printPathsRecur(k.right, rPath, pathlen, rh);
    }
   
    // Driver code
    public static void main (String[] args)
    {
       
        // Enter the binary tree ...
        //           1
        //         /   \    
        //        2     3
        //      /   \  
        //     4     5
        //      \   / \
        //       8 6   7
        //      /
        //     9
        GFG.Root = new Node(1);
        GFG.Root.left = new Node(2);
        GFG.Root.right = new Node(3);
        GFG.Root.left.left = new Node(4);
        GFG.Root.left.right = new Node(5);
        GFG.Root.left.right.left = new Node(6);
        GFG.Root.left.right.right = new Node(7);
        GFG.Root.left.left.right = new Node(8);
        GFG.Root.left.left.right.left = new Node(9);
        diameter(Root);
     
    }
}
 
// This code is contributed by rag2127


Python3




# Python3 program to print the longest
# leaf to leaf path
 
# Tree node structure used in the program
class Node:
     
    def __init__(self, x):
         
        self.data = x
        self.left = None
        self.right = None
 
# Function to find height of a tree
def height(root):
      
    global ans, k, lh, rh, f
     
    if (root == None):
        return 0
 
    left_height = height(root.left)
 
    right_height = height(root.right)
 
    # Update the answer, because diameter of a
    # tree is nothing but maximum value of
    # (left_height + right_height + 1) for each node
    if (ans < 1 + left_height + right_height):
        ans = 1 + left_height + right_height
 
        # Save the root, this will help us finding the
        # left and the right part of the diameter
        k = root
 
        # Save the height of left & right
        # subtree as well.
        lh = left_height
        rh = right_height
 
    return 1 + max(left_height, right_height)
 
# Prints the root to leaf path
def printArray(ints, lenn, f):
     
    # Print left part of the path
    # in reverse order
    if (f == 0):
        for i in range(lenn - 1, -1, -1):
            print(ints[i], end = " ")
 
    # Print right part of the path
    elif (f == 1):
        for i in range(lenn):
            print(ints[i], end = " ")
 
# This function finds out all the
# root to leaf paths
def printPathsRecur(node, path, maxm, pathlen):
     
    global f
 
    if (node == None):
        return
 
    # Append this node to the path array
    path[pathlen] = node.data
    pathlen += 1
 
    # If it's a leaf, so print the
    # path that led to here
    if (node.left == None and node.right == None):
         
        # Print only one path which is equal to the
        # height of the tree.
        # print(pathlen,"---",maxm)
        if (pathlen == maxm and (f == 0 or f == 1)):
             
            # print("innn")
            printArray(path, pathlen,f)
            f = 2
 
    else:
         
        # Otherwise try both subtrees
        printPathsRecur(node.left, path, maxm, pathlen)
        printPathsRecur(node.right, path, maxm, pathlen)
 
# Computes the diameter of a binary
# tree with given root.
def diameter(root):
     
    global ans, lh, rh, f, k, pathLen
 
    if (root == None):
        return
     
    # f is a flag whose value helps in printing
    # left & right part of the diameter only once
    height_of_tree = height(root)
    lPath = [0 for i in range(100)]
 
    # print(lh,"--",rh)
 
    # Print the left part of the diameter
    printPathsRecur(k.left, lPath, lh, 0);
    print(k.data, end = " ")
    rPath = [0 for i in range(100)]
    f = 1
 
    # Print the right part of the diameter
    printPathsRecur(k.right, rPath, rh, 0)
     
# Driver code
if __name__ == '__main__':
     
    k, lh, rh, f, ans, pathLen = None, 0, 0, 0, 0 - 10 ** 19, 0
     
    # Enter the binary tree ...
    #          1
    #        /   \
    #       2     3
    #     /   \
    #    4     5
    #     \   / \
    #      8 6   7
    #     /
    #    9
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
    root.left.right.left = Node(6)
    root.left.right.right = Node(7)
    root.left.left.right = Node(8)
    root.left.left.right.left = Node(9)
 
    diameter(root)
     
# This code is contributed by mohit kumar 29


C#




// C# program to print the longest leaf to leaf
// path
using System;
 
// Tree node structure used in the program
public class Node
{
    public int data;
    public Node left, right;
    public Node(int val)
    {
        data = val;
        left = right = null;
    }
}
 
public class GFG
{
    static int ans, lh, rh, f;
    static Node k;
    public static Node Root;
     
    // Function to find height of a tree
    static int height(Node root)
    {
        if (root == null)
            return 0;
        int left_height = height(root.left);
        int right_height = height(root.right);
        
        // update the answer, because diameter of a
        // tree is nothing but maximum value of
        // (left_height + right_height + 1) for each node
        if (ans < 1 + left_height + right_height)
        {
            ans = 1 + left_height + right_height;
   
            // save the root, this will help us finding the
            //  left and the right part of the diameter
            k = root;
   
            // save the height of left & right subtree as well.
            lh = left_height;
            rh = right_height;
        }
        return 1 + Math.Max(left_height, right_height);
  
    }
     
    // prints the root to leaf path
    static void printArray(int[] ints, int len)
    {
        int i;
     
        // print left part of the path in reverse order
        if(f == 0)
        {
            for(i = len - 1; i >= 0; i--)
            {
                Console.Write(ints[i] + " ");
            }
        }
        else if(f == 1)
        {
            for (i = 0; i < len; i++)
            {
                Console.Write(ints[i] + " ");  
            }
        }
    }
     
    // this function finds out all the root to leaf paths
    static void printPathsRecur(Node node, int[] path,int pathLen, int max)
    {
        if (node == null)
            return;
        // append this node to the path array
        path[pathLen] = node.data;
        pathLen++;
          
        // If it's a leaf, so print the path that led to here
        if (node.left == null && node.right == null)
        {
            
            // print only one path which is equal to the
            // height of the tree.
            if (pathLen == max && (f == 0 || f == 1))
            {
                printArray(path, pathLen);
                f = 2;
            }
        }
        else
        {
            
            // otherwise try both subtrees
        printPathsRecur(node.left, path, pathLen, max);
        printPathsRecur(node.right, path, pathLen, max);
        }
    }
     
    // Computes the diameter of a binary tree with given root.
    static void diameter(Node root)
    {
        if (root == null)
            return;
        
        // lh will store height of left subtree
        // rh will store height of right subtree
        ans = Int32.MinValue;
        lh = 0;
        rh = 0;
        
        // f is a flag whose value helps in printing
        // left & right part of the diameter only once
        f = 0;
        int height_of_tree= height(root);
         
          
        int[] lPath = new int[100];
        int pathlen = 0 * height_of_tree;
        
        // print the left part of the diameter
        printPathsRecur(k.left, lPath, pathlen, lh);
        Console.Write(k.data+" ");
        int[] rPath = new int[100];
        f = 1;
        
        // print the right part of the diameter
        printPathsRecur(k.right, rPath, pathlen, rh);
    }
     
    // Driver code
    static public void Main (){
        // Enter the binary tree ...
        //           1
        //         /   \    
        //        2     3
        //      /   \  
        //     4     5
        //      \   / \
        //       8 6   7
        //      /
        //     9
        GFG.Root = new Node(1);
        GFG.Root.left = new Node(2);
        GFG.Root.right = new Node(3);
        GFG.Root.left.left = new Node(4);
        GFG.Root.left.right = new Node(5);
        GFG.Root.left.right.left = new Node(6);
        GFG.Root.left.right.right = new Node(7);
        GFG.Root.left.left.right = new Node(8);
        GFG.Root.left.left.right.left = new Node(9);
        diameter(Root);
    }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript




<script>
// Javascript program to print the longest leaf to leaf
// path
 
// Tree node structure used in the program
class Node
{
    constructor(val)
    {
        this.data=val;
        this.left = this.right = null;
    }
}
 
let ans, lh, rh, f;
let  k;
let Root;
 
// Function to find height of a tree
function height(root)
{
    if (root == null)
        return 0;
    let left_height = height(root.left);
    let right_height = height(root.right);
        
    // update the answer, because diameter of a
    // tree is nothing but maximum value of
    // (left_height + right_height + 1) for each node
    if (ans < 1 + left_height + right_height)
    {
        ans = 1 + left_height + right_height;
   
        // save the root, this will help us finding the
        //  left and the right part of the diameter
        k = root;
   
        // save the height of left & right subtree as well.
        lh = left_height;
        rh = right_height;
    }
    return 1 + Math.max(left_height, right_height);
}
 
// prints the root to leaf path
function printArray(ints,len)
{
    let i;
     
    // print left part of the path in reverse order
    if(f == 0)
    {
        for(i = len - 1; i >= 0; i--)
        {
            document.write(ints[i] + " ");
        }
    }
    else if(f == 1)
    {
        for (i = 0; i < len; i++)
        {
            document.write(ints[i] + " "); 
        }
    }
}
 
// this function finds out all the root to leaf paths
function printPathsRecur(node,path,pathLen,max)
{
    if (node == null)
        return;
        
    // append this node to the path array
    path[pathLen] = node.data;
    pathLen++;
          
    // If it's a leaf, so print the path that led to here
    if (node.left == null && node.right == null)
    {
            
        // print only one path which is equal to the
        // height of the tree.
        if (pathLen == max && (f == 0 || f == 1))
        {
            printArray(path, pathLen);
            f = 2;
        }
    }
    else
    {
            
        // otherwise try both subtrees
        printPathsRecur(node.left, path, pathLen, max);
        printPathsRecur(node.right, path, pathLen, max);
    }
}
 
// Computes the diameter of a binary tree with given root.
function diameter(root)
{
    if (root == null)
        return;
        
    // lh will store height of left subtree
    // rh will store height of right subtree
    ans = Number.MIN_VALUE;
    lh = 0;
    rh = 0;
        
    // f is a flag whose value helps in printing
    // left & right part of the diameter only once
    f = 0;
    let height_of_tree = height(root);
          
    let lPath = new Array(100);
    let pathlen = 0;
        
    // print the left part of the diameter
    printPathsRecur(k.left, lPath, pathlen, lh);
    document.write(k.data+" ");
    let rPath = new Array(100);
    f = 1;
        
    // print the right part of the diameter
    printPathsRecur(k.right, rPath, pathlen, rh);
}
 
// Driver code
 
// Enter the binary tree ...
//           1
//         /   \   
//        2     3
//      /   \ 
//     4     5
//      \   / \
//       8 6   7
//      /
//     9
Root = new Node(1);
Root.left = new Node(2);
Root.right = new Node(3);
Root.left.left = new Node(4);
Root.left.right = new Node(5);
Root.left.right.left = new Node(6);
Root.left.right.right = new Node(7);
Root.left.left.right = new Node(8);
Root.left.left.right.left = new Node(9);
diameter(Root);
     
 
// This code is contributed by patel2127
</script>


Output

9 8 4 2 5 6 



Time complexity: O(n) where n is size of binary tree

Auxiliary Space: O(h) where h is  the height of binary tree.

Another Approach:
First define a function which print all possible longest paths through root of the tree
1.Find nodes at deepest level of left sub tree
2.Find nodes at deepest level of right sub tree
3.Print all possible paths from deepest nodes of left sub tree to deepest nodes of left sub tree

Second Define a function to print all the longest paths of the tree
1. find the diameter of the tree and pass it as argument of the function
2. Calculate left sub-tree height + right sub-tree height +1
3. if (left sub-tree height + right sub-tree height +1) is equal with diameter of tree
then longest path exist through root of the tree. Call the function which print
all possible longest paths through root of the tree for current tree.
4. Then call the same function to print all longest paths for its left sub tree and
right sub tree
5. Continue the above steps for all sub trees of the tree

C++




// C++ code addition
#include <bits/stdc++.h>
using namespace std;
 
class Node {
public:
    Node* left;
    Node* right;
    int value;
 
    Node(int value) {
        left = NULL;
        right = NULL;
        this->value = value;
    }
};
 
// create a function to find deepest nodes of the given tree
class DeepestNodes {
public:
    vector<Node*> nodes;
 
    void deepest(Node* root, int level) {
        if (root == NULL) {
            return;
        }
 
        if (level == 1) {
            nodes.push_back(root);
        }
        else if (level > 1) {
            deepest(root->left, level - 1);
            deepest(root->right, level - 1);
        }
    }
};
 
// create a function to calculate height of the given tree
int height(Node* root) {
    if (root == NULL) {
        return 0;
    }
    return max(height(root->left), height(root->right)) + 1;
}
 
// create a function to calculate diameter of the given tree
int diameter(Node* root) {
    if (root == NULL) {
        return 0;
    }
    int l_height = height(root->left);
    int r_height = height(root->right);
    return max(l_height + r_height + 1, max(diameter(root->left), diameter(root->right)));
}
 
// create a function to print the path from root to given node
// flag=0 in order and flag=-1 reversed order
void print_to_node(vector<int> stack, Node* root, Node* target, int flag) {
     // exit condition
    if (root == NULL) {
        return;
    }
 
    stack.push_back(root->value);
    if (root->value == target->value) {
        // print stack as we hit the target node
        if (flag == 0) {
            for (int i = 1; i < stack.size(); i++) {
                cout << stack[i] << " ";
            }
        }
        else if (flag == -1) {
            for (int i = stack.size() - 1; i >= 0; i--) {
                cout << stack[i] << " ";
            }
        }
    }
 
    // call recursive
    print_to_node(stack, root->left, target, flag);
    print_to_node(stack, root->right, target, flag);
    stack.pop_back();
}
 
// create a function to print all possible longest paths through root of the tree
void print_path(Node* root) {
    DeepestNodes nodes_left;
    nodes_left.deepest(root->left, height(root->left));
    vector<Node*> l_deepest = nodes_left.nodes;
 
    DeepestNodes nodes_right;
    nodes_right.deepest(root->right, height(root->right));
    vector<Node*> r_deepest = nodes_right.nodes;
 
    if (l_deepest.size() == 0) {
        // only right deepest nodes exist
        for (int i = 0; i < r_deepest.size(); i++) {
            Node* right = r_deepest[i];
            print_to_node(vector<int>(), root, right, 0);
            cout << endl;
        }
        return;
    }
 
    if (r_deepest.size() == 0) {
        for (int i = 0; i < l_deepest.size(); i++) {
            Node* left = l_deepest[i];
            print_to_node(vector<int>(), root, left, -1);
            cout << endl;
        }
        return;
    }
 
    for (int i = 0; i < l_deepest.size(); i++) {
        for (int j = 0; j < r_deepest.size(); j++) {
            Node* left = l_deepest[i];
            Node* right = r_deepest[j];
 
            print_to_node(vector<int>(), root, left, -1);
            print_to_node(vector<int>(), root, right, 0);
            cout << endl;
        }
    }
}
 
// create a function to print longest path leaf to leaf
void print_longest_path(Node* root, int diameter_tree) {
    if(root == NULL) {
        return;
    }
     
    int height_sum = height(root->left) + height(root->right) + 1;
     
    if (height_sum == diameter_tree) {
        print_path(root);
        return;
    }
     
    if(root->left) {
        print_longest_path(root->left, diameter_tree);
    }
    if(root->right) {
        print_longest_path(root->right, diameter_tree);
    }
}
int main() {
     
// # test case
// """
//  Enter the binary tree ...
//                1
//              /   \    
//             2     3
//           /   \    \
//          4     5    11
//           \   / \
//            8 6   7
//           /       \
//          9        10
// """
 
    Node* root_node = new Node(1);
    root_node->left = new Node(2);
    root_node->right = new Node(3);
    root_node->right->right = new Node(11);
    root_node->left->left = new Node(4);
    root_node->left->left->right = new Node(8);
    root_node->left->left->right->left = new Node(9);
    root_node->left->right = new Node(5);
    root_node->left->right->left = new Node(6);
    root_node->left->right->right = new Node(7);
    root_node->left->right->right->right = new Node(10);
     
    cout << "Longest paths in tree:" << endl;
    print_longest_path(root_node, diameter(root_node));
    return 0;
}
 
// The code is contributed by Utkarsh


Java




import java.util.ArrayList;
import java.util.List;
 
class Node {
    Node left;
    Node right;
    int value;
 
    public Node(int value) {
        this.value = value;
        left = null;
        right = null;
    }
}
 
class DeepestNodes {
    List<Node> nodes = new ArrayList<>();
 
    public void deepest(Node root, int level) {
        if (root == null) {
            return;
        }
 
        if (level == 1) {
            nodes.add(root);
        } else if (level > 1) {
            deepest(root.left, level - 1);
            deepest(root.right, level - 1);
        }
    }
}
 
public class BinaryTreeLongestPaths {
 
    // Function to calculate the height of the tree
    public static int height(Node root) {
        if (root == null) {
            return 0;
        }
        return Math.max(height(root.left), height(root.right)) + 1;
    }
 
    // Function to calculate the diameter of the tree
    public static int diameter(Node root) {
        if (root == null) {
            return 0;
        }
        int lHeight = height(root.left);
        int rHeight = height(root.right);
        return Math.max(lHeight + rHeight + 1,
                        Math.max(diameter(root.left), diameter(root.right)));
    }
 
    // Function to print the path from root to a given node
    public static void printToNode(List<Integer> stack,
                                   Node root, Node target, int flag) {
        if (root == null) {
            return;
        }
 
        stack.add(root.value);
        if (root.value == target.value) {
            if (flag == 0) {
                for (int i = 1; i < stack.size(); i++) {
                    System.out.print(stack.get(i) + " ");
                }
            } else if (flag == -1) {
                for (int i = stack.size() - 1; i >= 0; i--) {
                    System.out.print(stack.get(i) + " ");
                }
            }
        }
 
        printToNode(stack, root.left, target, flag);
        printToNode(stack, root.right, target, flag);
        stack.remove(stack.size() - 1);
    }
 
    // Function to print all possible longest paths through the root of the tree
    public static void printPath(Node root) {
        DeepestNodes nodesLeft = new DeepestNodes();
        nodesLeft.deepest(root.left, height(root.left));
        List<Node> lDeepest = nodesLeft.nodes;
 
        DeepestNodes nodesRight = new DeepestNodes();
        nodesRight.deepest(root.right, height(root.right));
        List<Node> rDeepest = nodesRight.nodes;
 
        if (lDeepest.size() == 0) {
            for (Node right : rDeepest) {
                printToNode(new ArrayList<>(), root, right, 0);
                System.out.println();
            }
            return;
        }
 
        if (rDeepest.size() == 0) {
            for (Node left : lDeepest) {
                printToNode(new ArrayList<>(), root, left, -1);
                System.out.println();
            }
            return;
        }
 
        for (Node left : lDeepest) {
            for (Node right : rDeepest) {
                printToNode(new ArrayList<>(), root, left, -1);
                printToNode(new ArrayList<>(), root, right, 0);
                System.out.println();
            }
        }
    }
 
    // Function to print the longest path from leaf to leaf
    public static void printLongestPath(Node root, int diameterTree) {
        if (root == null) {
            return;
        }
 
        int heightSum = height(root.left) + height(root.right) + 1;
 
        if (heightSum == diameterTree) {
            printPath(root);
            return;
        }
 
        if (root.left != null) {
            printLongestPath(root.left, diameterTree);
        }
        if (root.right != null) {
            printLongestPath(root.right, diameterTree);
        }
    }
 
    public static void main(String[] args) {
        /* Create a binary tree:
               1
              / \
             2   3
            / \   \
           4   5   11
            \  / \
            8 6 7
             /   \
            9    10
         */
        Node rootNode = new Node(1);
        rootNode.left = new Node(2);
        rootNode.right = new Node(3);
        rootNode.right.right = new Node(11);
        rootNode.left.left = new Node(4);
        rootNode.left.left.right = new Node(8);
        rootNode.left.left.right.left = new Node(9);
        rootNode.left.right = new Node(5);
        rootNode.left.right.left = new Node(6);
        rootNode.left.right.right = new Node(7);
        rootNode.left.right.right.right = new Node(10);
 
        System.out.println("Longest paths in the tree:");
        printLongestPath(rootNode, diameter(rootNode));
    }
}


Python3




# THIS CODE IS CONTRIBUTED BY KIRTI AGARWAL
class Node:
    def __init__(self, value):
        self.left = None
        self.right = None
        self.value = value
 
# create a function to calculate height of the given tree
def height(root):
    if root is None:
        return 0
    return max(height(root.left), height(root.right)) + 1
 
# create a function to find deepest nodes of the given tree
class DeepestNodes(object):
 
    def __init__(self):
        self.nodes = []
 
    def deepest(self, root, level):
        if root is None:
            return
        if level == 1:
            self.nodes.append(root)
        elif level > 1:
            self.deepest(root.left, level - 1)
            self.deepest(root.right, level - 1)
 
 
# create a function to calculate diameter of the given tree
def diameter(root):
    if root is None:
        return 0
    l_height = height(root.left)
    r_height = height(root.right)
    return max(l_height + r_height + 1,
               diameter(root.left), diameter(root.right))
 
 
# create a function to print the path from root to given node
# flag=0 in order and flag=-1 reversed order
def print_to_node(stack, root, target, flag):
    if root is None# exit condition
        return
    stack.append(root.value)
    if root.value == target.value:  # print stack as we hit the target node
        if flag == 0:
            for i in range(len(stack)):
                print(stack[i], end=" ")
        elif flag == -1:
            for i in range(len(stack) - 1):
                print(stack[len(stack) - 1 - i], end=" ")
    # call recursive
    print_to_node(stack, root.left, target, flag)
    print_to_node(stack, root.right, target, flag)
    stack.pop()
 
 
# create a function to print all possible longest paths through root of the tree
def print_path(root):
    nodes_left = DeepestNodes()
    nodes_left.deepest(root.left, height(root.left))
    l_deepest = nodes_left.nodes
    # print("l_deepest:", [e.value for e in l_deepest])
    nodes_right = DeepestNodes()
    nodes_right.deepest(root.right, height(root.right))
    r_deepest = nodes_right.nodes
    # print("r_deepest:", [e.value for e in r_deepest])
    if len(l_deepest) == 0:
        # only right deepest nodes exist
        for right in r_deepest:
            print_to_node([], root, right, 0)
            print()
        return
    if len(r_deepest) == 0:
        # only left deepest nodes exist
        for left in l_deepest:
            print_to_node([], root, left, -1)
            print()
        return
    for left in l_deepest:
        for right in r_deepest:
            print_to_node([], root, left, -1)
            print_to_node([], root, right, 0)
            print()
 
 
# create a function to print longest path leaf to leaf
def print_longest_path(root, diameter_tree):
    if root is None:
        return
 
    height_sum = height(root.left) + height(root.right) + 1
 
    if height_sum == diameter_tree:
        print_path(root)
        return
    if root.left:
        print_longest_path(root.left, diameter_tree)
    if root.right:
        print_longest_path(root.right, diameter_tree)
 
 
# test case
"""
 Enter the binary tree ...
               1
             /   \     
            2     3
          /   \    \ 
         4     5    11
          \   / \ 
           8 6   7
          /       \
         9        10
"""
root_node = Node(1)
root_node.left = Node(2)
root_node.right = Node(3)
 
root_node.right.right = Node(11)
 
root_node.left.left = Node(4)
root_node.left.left.right = Node(8)
root_node.left.left.right.left = Node(9)
 
root_node.left.right = Node(5)
root_node.left.right.left = Node(6)
root_node.left.right.right = Node(7)
root_node.left.right.right.right = Node(10)
 
print("Longest paths in tree")
print_longest_path(root_node, diameter(root_node))


C#




using System;
using System.Collections.Generic;
 
// Define a class to represent a binary tree node
class Node
{
    public Node left;
    public Node right;
    public int value;
 
    public Node(int value)
    {
        left = null;
        right = null;
        this.value = value;
    }
}
 
// Define a class to find and store the deepest nodes in the tree
class DeepestNodes
{
    public List<Node> nodes = new List<Node>();
 
    // Recursively find and store the deepest nodes at the given level
    public void Deepest(Node root, int level)
    {
        if (root == null)
        {
            return;
        }
 
        if (level == 1)
        {
            nodes.Add(root);
        }
        else if (level > 1)
        {
            Deepest(root.left, level - 1);
            Deepest(root.right, level - 1);
        }
    }
}
 
// Define a class to represent the binary tree and various tree-related functions
class BinaryTree
{
    // Stack to keep track of the path from root to a given node
    public List<int> stack = new List<int>();
 
    // Find and store the deepest nodes in the left and right subtrees
    public void DeepestNodesUtil(Node root, int level)
    {
        if (root == null)
        {
            return;
        }
 
        if (level == 1)
        {
            stack.Add(root.value);
        }
        else if (level > 1)
        {
            DeepestNodesUtil(root.left, level - 1);
            DeepestNodesUtil(root.right, level - 1);
        }
    }
 
    // Calculate the height of the tree
    public int Height(Node root)
    {
        if (root == null)
        {
            return 0;
        }
        return Math.Max(Height(root.left), Height(root.right)) + 1;
    }
 
    // Calculate the diameter of the tree
    public int Diameter(Node root)
    {
        if (root == null)
        {
            return 0;
        }
        int lHeight = Height(root.left);
        int rHeight = Height(root.right);
        return Math.Max(lHeight + rHeight + 1, Math.Max(Diameter(root.left), Diameter(root.right)));
    }
 
    // Print the path from root to a given node
    public void PrintToNode(Node root, Node target, int flag)
    {
        if (root == null)
        {
            return;
        }
 
        stack.Add(root.value);
 
        if (root.value == target.value)
        {
            if (flag == 0)
            {
                for (int i = 1; i < stack.Count; i++)
                {
                    Console.Write(stack[i] + " ");
                }
            }
            else if (flag == -1)
            {
                for (int i = stack.Count - 1; i >= 0; i--)
                {
                    Console.Write(stack[i] + " ");
                }
            }
        }
 
        PrintToNode(root.left, target, flag);
        PrintToNode(root.right, target, flag);
        stack.RemoveAt(stack.Count - 1);
    }
 
    // Print all possible longest paths through the root of the tree
    public void PrintPath(Node root)
    {
        DeepestNodes nodesLeft = new DeepestNodes();
        nodesLeft.Deepest(root.left, Height(root.left));
        List<Node> lDeepest = nodesLeft.nodes;
 
        DeepestNodes nodesRight = new DeepestNodes();
        nodesRight.Deepest(root.right, Height(root.right));
        List<Node> rDeepest = nodesRight.nodes;
 
        if (lDeepest.Count == 0)
        {
            for (int i = 0; i < rDeepest.Count; i++)
            {
                Node right = rDeepest[i];
                PrintToNode(root, right, 0);
                Console.WriteLine();
            }
            return;
        }
 
        if (rDeepest.Count == 0)
        {
            for (int i = 0; i < lDeepest.Count; i++)
            {
                Node left = lDeepest[i];
                PrintToNode(root, left, -1);
                Console.WriteLine();
            }
            return;
        }
 
        for (int i = 0; i < lDeepest.Count; i++)
        {
            for (int j = 0; j < rDeepest.Count; j++)
            {
                Node left = lDeepest[i];
                Node right = rDeepest[j];
                PrintToNode(root, left, -1);
                PrintToNode(root, right, 0);
                Console.WriteLine();
            }
        }
    }
 
    // Find and print the longest paths in the tree
    public void PrintLongestPath(Node root, int diameterTree)
    {
        if (root == null)
        {
            return;
        }
 
        int heightSum = Height(root.left) + Height(root.right) + 1;
 
        if (heightSum == diameterTree)
        {
            PrintPath(root);
            return;
        }
 
        if (root.left != null)
        {
            PrintLongestPath(root.left, diameterTree);
        }
 
        if (root.right != null)
        {
            PrintLongestPath(root.right, diameterTree);
        }
    }
 
    public static void Main(string[] args)
    {
        // Create a sample binary tree
        Node root_node = new Node(1);
        root_node.left = new Node(2);
        root_node.right = new Node(3);
        root_node.right.right = new Node(11);
        root_node.left.left = new Node(4);
        root_node.left.left.right = new Node(8);
        root_node.left.left.right.left = new Node(9);
        root_node.left.right = new Node(5);
        root_node.left.right.left = new Node(6);
        root_node.left.right.right = new Node(7);
        root_node.left.right.right.right = new Node(10);
 
        BinaryTree tree = new BinaryTree();
        Console.WriteLine("Longest paths in tree:");
        tree.PrintLongestPath(root_node, tree.Diameter(root_node));
    }
}


Javascript




// javascript code addition
class Node{
    constructor(value){
        this.left = null;
        this.right = null;
        this.value = value;
    }
}
 
// create a function to calculate height of the given tree
function height(root){
    if(root == null) return 0;
    return Math.max(height(root.left), height(root.right)) + 1;
}
 
// create a function to find deepest nodes of the given tree
class DeepestNodes{
    constructor(){
        this.nodes = [];
    }
     
    deepest(root, level){
        if(root == null) return;
         
        if(level == 1){
            this.nodes.push(root);
        }
        else if(level > 1){
            this.deepest(root.left, level - 1);
            this.deepest(root.right, level - 1);
        }
    }
}
 
 
// create a function to calculate diameter of the given tree
function diameter(root){
    if(root == null) return 0;
    let l_height = height(root.left);
    let r_height = height(root.right);
    return Math.max(l_height + r_height + 1, diameter(root.left), diameter(root.right));
}
 
 
// create a function to print the path from root to given node
// flag=0 in order and flag=-1 reversed order
 
function print_to_node(stack, root, target, flag){
    // exit condition
    if(root == null) return;
     
    stack.push(root.value)
    if(root.value == target.value){
        // print stack as we hit the target node
        if(flag == 0)
            for(let i = 0; i < stack.length; i++){
                console.log(stack[i]);
            }
        else if(flag == -1)
            for(let i= 0; i < stack.length - 1; i++){
                console.log(stack[stack.length - 1 - i]);
            }
    
     
     
    // call recursive
    print_to_node(stack, root.left, target, flag)
    print_to_node(stack, root.right, target, flag)
    stack.pop()
}
 
// create a function to print all possible longest paths through root of the tree
function print_path(root){
    let nodes_left = new DeepestNodes();
    nodes_left.deepest(root.left, height(root.left));
    let l_deepest = nodes_left.nodes;
     
    // print("l_deepest:", [e.value for e in l_deepest])
    let nodes_right = new DeepestNodes();
    nodes_right.deepest(root.right, height(root.right));
    let r_deepest = nodes_right.nodes;
    // print("r_deepest:", [e.value for e in r_deepest])
    if(l_deepest.length == 0){
        // only right deepest nodes exist
        for(let i = 0; i < r_deepest.length; i++){
            let right = r_deepest[i];
            print_to_node([], root, right, 0);
            console.log();           
        }
 
        return;
    }
    if(r_deepest.length == 0){
        for(let i = 0; i < l_deepest.length; i++){
            let left = l_deepest[i];
            print_to_node([], root, left, -1)
            console.log()
        }
        return;
    }
 
    for(let i = 0; i < l_deepest.length; i++){
        for(let j = 0; j < r_deepest.length; j++){
            let left = l_deepest[i];
            let right = r_deepest[j];
             
            print_to_node([], root, left, -1)
            print_to_node([], root, right, 0)
            console.log()
        }
    }
}
 
// create a function to print longest path leaf to leaf
function print_longest_path(root, diameter_tree){
    if(root ==null)
        return;
 
    let height_sum = height(root.left) + height(root.right) + 1
 
    if (height_sum == diameter_tree){
        print_path(root);
        return;
    }
 
    if(root.left)
        print_longest_path(root.left, diameter_tree);
    if(root.right)
        print_longest_path(root.right, diameter_tree); 
}
 
 
 
// # test case
// """
//  Enter the binary tree ...
//                1
//              /   \     
//             2     3
//           /   \    \ 
//          4     5    11
//           \   / \ 
//            8 6   7
//           /       \
//          9        10
// """
 
root_node = new Node(1);
root_node.left = new Node(2);
root_node.right = new Node(3);
 
root_node.right.right = new Node(11);
 
root_node.left.left = new Node(4);
root_node.left.left.right = new Node(8);
root_node.left.left.right.left = new Node(9);
 
root_node.left.right = new Node(5);
root_node.left.right.left = new Node(6);
root_node.left.right.right = new Node(7);
root_node.left.right.right.right = new Node(10);
 
console.log("Longest paths in tree");
print_longest_path(root_node, diameter(root_node));
 
// The code is contributed by Nidhi goel.


Output

Longest paths in tree
9 8 4 2 1 3 11 
10 7 5 2 1 3 11 



Time Complexity: O(N) where N is the number of nodes in given binary tree

Auxiliary Space: O(h) due to recursion call stack



Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads