Listing the paths to leaves in a tree

后端 未结 3 767
失恋的感觉
失恋的感觉 2021-02-10 06:18

I\'m trying to write a function that finds all of the paths to the leaves in a tree. For example, given a tree that looks like this:

           1
         /  \\
         


        
3条回答
  •  心在旅途
    2021-02-10 06:57

    It's simple...These are the things that you need to have in mind :-

    1. Recursively call the function
    2. Within the function the traversal should be in the form of PreOrder ( root, left and then right)
    3. Use an array within the recursive function and store the value of every node visited.
    4. If the visited node is a leaf ... node--> left == NULL and node--> right == NULL. Then output the contents of the array.

提交回复
热议问题