原文:

给定一个 ,任务是。

示例:

输入: 1 /\ 2 3 /\ 4 5 6 /\ 7 8

产量:t2【1 2 4】t3【1 3 5】t4【1 3 6 7】t5【1 3 6 8】

输入: 1 /| \ 2 5 3 /\ \ 4 5 6 输出: 1 2 4 1 2 5 1 5 1 3 6

方法:解决这个问题的思路是使用开始,并保持 。每当遇到叶节点时,打印存储在中的元素,作为当前遍历的根到叶路径,删除最后添加的叶,并检查下一个组合。

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
// structure of an n ary tree node
class node {
public:
    int data;
    vector child;
    // parameterized constructor
    node(int x)
        : data(x)
    {
    }
};
// function to print the root to leaf
// path of the given n-ary tree
void printpath(vector vec)
{
    // print elements in the vector
    for (int ele : vec) {
        cout << ele << " ";
    }
    cout << endl;
}
// utility function to print all
// root to leaf paths of an nary tree
void printallroottoleafpaths(
    node* root, vector vec)
{
    // if root is null
    if (!root)
        return;
    // insert current node's
    // data into the vector
    vec.push_back(root->data);
    // if current node is a leaf node
    if (root->child.empty()) {
        // print the path
        printpath(vec);
        // pop the leaf node
        // and return
        vec.pop_back();
        return;
    }
    // recur for all children of
    // the current node
    for (int i = 0;
        i < root->child.size(); i  )
        // recursive function call
        printallroottoleafpaths(
            root->child[i], vec);
}
// function to print root to leaf path
void printallroottoleafpaths(node* root)
{
    // if root is null, return
    if (!root)
        return;
    // stores the root to leaf path
    vector vec;
    // utility function call
    printallroottoleafpaths(root, vec);
}
// driver code
int main()
{
    // given n-ary tree
    node* root = new node(1);
    (root->child).push_back(new node(2));
    (root->child).push_back(new node(3));
    (root->child[0]->child).push_back(new node(4));
    (root->child[1]->child).push_back(new node(5));
    (root->child[1]->child).push_back(new node(6));
    (root->child[1]->child[1]->child)
        .push_back(new node(7));
    (root->child[1]->child[1]->child)
        .push_back(new node(8));
    // function call
    printallroottoleafpaths(root);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program for the above approach
import java.util.arraylist;
class gfg
{
    // structure of an n ary tree node
    static class node
    {
        int data;
        arraylist child;
        // parameterized constructor
        public node(int x)
        {
            this.data = x;
            this.child = new arraylist<>();
        }
    };
    // function to print the root to leaf
    // path of the given n-ary tree
    static void printpath(arraylist vec)
    {
        // print elements in the vector
        for (int ele : vec)
        {
            system.out.print(ele   " ");
        }
        system.out.println();
    }
    // utility function to print all
    // root to leaf paths of an nary tree
    static void printallroottoleafpaths(node root, arraylist vec)
    {
        // if root is null
        if (root == null)
            return;
        // insert current node's
        // data into the vector
        vec.add(root.data);
        // if current node is a leaf node
        if (root.child.isempty())
        {
            // print the path
            printpath(vec);
            // pop the leaf node
            // and return
            vec.remove(vec.size() - 1);
            return;
        }
        // recur for all children of
        // the current node
        for (int i = 0; i < root.child.size(); i  )
            // recursive function call
            printallroottoleafpaths(root.child.get(i), vec);
        vec.remove(vec.size() - 1);
    }
    // function to print root to leaf path
    static void printallroottoleafpaths(node root)
    {
        // if root is null, return
        if (root == null)
            return;
        // stores the root to leaf path
        arraylist vec = new arraylist<>();
        // utility function call
        printallroottoleafpaths(root, vec);
    }
    // driver code
    public static void main(string[] args)
    {
        // given n-ary tree
        node root = new node(1);
        (root.child).add(new node(2));
        (root.child).add(new node(3));
        (root.child.get(0).child).add(new node(4));
        (root.child.get(1).child).add(new node(5));
        (root.child.get(1).child).add(new node(6));
        (root.child.get(1).child.get(1).child).add(new node(7));
        (root.child.get(1).child.get(1).child).add(new node(8));
        // function call
        printallroottoleafpaths(root);
    }
}
// this code is contributed by sanjeev2552

python 3

# python3 program for the above approach
# structure of an n ary tree node
class node:
    def __init__(self, x):
        self.data = x
        self.child = []
# function to print the root to leaf
# path of the given n-ary tree
def printpath(vec):
    # print elements in the vector
    for ele in vec:
        print(ele, end = " ")
    print()
# utility function to print all
# root to leaf paths of an nary tree
def printallroottoleafpaths(root):
    global vec
    # if root is null
    if (not root):
        return
    # insert current node's
    # data into the vector
    vec.append(root.data)
    # if current node is a leaf node
    if (len(root.child) == 0):
        # print the path
        printpath(vec)
        # pop the leaf node
        # and return
        vec.pop()
        return
    # recur for all children of
    # the current node
    for i in range(len(root.child)):
        # recursive function call
        printallroottoleafpaths(root.child[i])
    vec.pop()   
# function to print root to leaf path
def printroottoleafpaths(root):
    global vec
    # if root is null, return
    if (not root):
        return
    # utility function call
    printallroottoleafpaths(root)
# driver code
if __name__ == '__main__':
    # given n-ary tree
    vec = []
    root = node(1)
    root.child.append(node(2))
    root.child.append(node(3))
    root.child[0].child.append(node(4))
    root.child[1].child.append(node(5))
    root.child[1].child.append(node(6))
    root.child[1].child[1].child.append(node(7))
    root.child[1].child[1].child.append(node(8))
    # function call
    printroottoleafpaths(root)
# this code is contributed by mohit kumar 29

c

using system;
using system.collections.generic;
// structure of an n ary tree node
public class node
{
    public int data;
    public list child;
    // parameterized constructor
    public node(int x)
    {
        this.data = x;
        this.child = new list();
    }
}
public class gfg
{
    // function to print the root to leaf
    // path of the given n-ary tree
    static void printpath(list vec)
    {
        // print elements in the vector
        foreach (int ele in vec)
        {
            console.write(ele   " ");
        }
        console.writeline();
    }
    // utility function to print all
    // root to leaf paths of an nary tree
    static void printallroottoleafpaths(node root, list vec)
    {
        // if root is null
        if (root == null)
            return;
        // insert current node's
        // data into the vector
        vec.add(root.data);
        // if current node is a leaf node
        if (root.child.count == 0)
        {
            // print the path
            printpath(vec);
            // pop the leaf node
            // and return
            vec.removeat(vec.count - 1);
            return;
        }
        // recur for all children of
        // the current node
        for (int i = 0; i < root.child.count; i  )
        {
            // recursive function call
            printallroottoleafpaths(root.child[i], vec);
        }
        vec.removeat(vec.count - 1);
    }
    // function to print root to leaf path
    static void printallroottoleafpaths(node root)
    {
        // if root is null, return
        if (root == null)
            return;
        // stores the root to leaf path
        list vec = new list();
        // utility function call
        printallroottoleafpaths(root, vec);
    }
    // driver code
    static public void main ()
    {
        // given n-ary tree
        node root = new node(1);
        (root.child).add(new node(2));
        (root.child).add(new node(3));
        (root.child[0].child).add(new node(4));
        (root.child[1].child).add(new node(5));
        (root.child[1].child).add(new node(6));
        (root.child[1].child[1].child).add(new node(7));
        (root.child[1].child[1].child).add(new node(8));
        // function call
        printallroottoleafpaths(root);
    }
}
// this code is contributed by rag2127

java 描述语言


output: 

1 2 4 
1 3 5 
1 3 6 7 
1 3 6 8

时间复杂度: o(n) 空间复杂度: o(n)