C++ 查找树中最长的根到叶路径

C++ 查找树中最长的根到叶路径,c++,tree,binary-tree,code-reuse,longest-path,C++,Tree,Binary Tree,Code Reuse,Longest Path,我需要找到树上最长的根到叶的路径。 我已经有了找到从根到叶的最短路径的代码,我想知道是否可以以任何方式重用它来找到最长的路径 // Recursive function used by leftMostShortest // to print the first shortest root to leaf path void AlberoCircuito::printPath(string Data, unordered_map<string,

我需要找到树上最长的根到叶的路径。 我已经有了找到从根到叶的最短路径的代码,我想知道是否可以以任何方式重用它来找到最长的路径

// Recursive function used by leftMostShortest
// to print the first shortest root to leaf path
void AlberoCircuito::printPath(string Data, unordered_map<string,
                             string> parent)
{
    // If the root's data is same as
    // its parent data then return
    if (parent[Data] == Data)
        return;

    // Recur for the function printPath
    printPath(parent[Data], parent);

    // Print the parent node's data
    pathMinimo.push_back(parent[Data]);
}


// Function to perform level order traversal
// until we find the first leaf node
vector<string> AlberoCircuito::leftMostShortest(NodoAlbero* root)
{
    // Queue to store the nodes
    queue<NodoAlbero*> q;

    // Push the root node
    q.push(root);

    // Initialize the value of first
    // leaf node to occur as -1
    string LeafData = " ";

    // To store the current node
    NodoAlbero* temp = NULL;

    // Map to store the parent node's data
    unordered_map<string, string> parent;

    // Parent of root data is set as it's
    // own value
    parent[root->value] = root->value;

    // We store first node of the smallest level
    while (!q.empty()) {
        temp = q.front();
        q.pop();

        // If the first leaf node has been found
        // set the flag variable as 1
        if (!temp->leftPtr && !temp->rightPtr) {
            LeafData = temp->value;
            break;
        }
        else {

            // If current node has left
            // child, push in the queue
            if (temp->leftPtr) {
                q.push(temp->leftPtr);

                // Set temp's left node's parent as temp
                parent[temp->leftPtr->value] = temp->value;
            }

            // If current node has right
            // child, push in the queue
            if (temp->rightPtr) {
                q.push(temp->rightPtr);

                // Set temp's right node's parent
                // as temp
                parent[temp->rightPtr->value] = temp->value;
            }
        }
    }

    // Recursive function to print the first
    // shortest root to leaf path
    printPath(LeafData, parent);

    // Print the leaf node of the first
    // shortest path
    pathMinimo.push_back(LeafData);
    return pathMinimo;
}
//leftMostShortest使用的递归函数
//打印第一条最短的根到叶路径
void alberocuito::printPath(字符串数据,无序映射父对象)
{
//如果根目录的数据与
//然后返回其父数据
if(父[数据]==数据)
返回;
//函数printPath的递归
打印路径(父[数据],父);
//打印父节点的数据
路径最小推回(父[数据]);
}
//用于执行级别顺序遍历的函数
//直到找到第一个叶节点
向量alberocuito::leftMostShortest(NodoAlbero*根)
{
//用于存储节点的队列
队列q;
//推送根节点
q、 推(根);
//初始化first的值
//叶节点将作为-1出现
字符串leaftdata=“”;
//存储当前节点的步骤
NodoAlbero*temp=NULL;
//映射以存储父节点的数据
无序映射父对象;
//根数据的父级设置为
//自身价值
父级[根->值]=根->值;
//我们存储最小级别的第一个节点
而(!q.empty()){
温度=q.前();
q、 pop();
//如果已找到第一个叶节点
//将标志变量设置为1
如果(!temp->leftPtr&&!temp->rightpr){
LeafData=temp->value;
打破
}
否则{
//如果当前节点已离开
//孩子,排队
如果(临时->左PTR){
q、 按下(温度->左PTR);
//将temp左侧节点的父节点设置为temp
父项[temp->leftPtr->value]=temp->value;
}
//如果当前节点有权限
//孩子,排队
如果(临时->右PTR){
q、 按下(温度->右侧PTR);
//设置临时节点的右侧节点的父节点
//临时工
父项[temp->rightPtr->value]=temp->value;
}
}
}
//递归函数来打印第一个
//最短根叶路径
打印路径(LeafData,父级);
//打印第一个节点的叶节点
//最短路径
路径最小推回(LeafData);
返回路径极小值;
}
这就是我用来找到最短路径并将其存储在向量中的代码。这些是一个班级的成员


有没有办法修改它并重用它?还是你认为我必须重新开始?谢谢:)

如果你真的想重用代码,你就不能在找到休假时不中断,而是继续处理节点吗?您知道,最后一个节点将使我成为路径最长的节点之一。但是,我会做一个简单的递归函数,其中当前节点的最长路径是1+左节点和右节点之间的最长路径。我不确定我是否理解你在注释的第二部分中的意思,因为我在编码方面很糟糕,但这不是给我树的高度吗?这不是你要找的吗?