Recursion with return values

Source: Internet
Author: User

A function can have only one return value. If a recursive function with a return value is called concurrently multiple times, multiple return values are generated. This is a bug. Therefore, in recursion with tree-like multi-branch structures, void is generally used as the return value type. The form parameters are transmitted as values in each path, and these values are saved or compared at the exit. For example:

 1 struct Node{ 2     int val; 3     Node* child; 4     Node* sibling; 5     Node(){ 6         child=NULL; 7         sibling=NULL; 8     } 9 }; 10 int height(Node *root){11     if(root){12         int maxh=0;13         for(Node *p=root;p;p=p->sibling){14             int tmp=height(p->child);15             if(maxh<tmp){16                 maxh=tmp;17             }18         }19         return maxh+1;20     }21     return 0;22 }

 

 1 int maxh=0; 2 void height(Node *root,int h){ 3     if(root){ 4         for(Node* p=root;p;p=p->sibling){ 5             height(p->child,h+1); 6         } 7     }else{ 8         if(maxh

This is because tree height requires each entry.PathThe deepest, and then compare, is to the deepest to be determined. It is also the only solution. In addition to these recursive problems with unique solutions, such as the multiple-solution problem represented by Queen N, or multi-branch-level problems, such as recursive Binary Tree creation, it is difficult to use recursive solutions with return values. Recursion with return values, from the shortest to the deep. After reaching the exit at the deepest point, the shortest layer can be returned.

A key point is the process of multi-branch recursion. As mentioned in the previous article, the recursive problem of too many branches is used to generate a tree structure. Therefore, we can refer to the three recursive traversal methods of Binary Trees and the DFS in graph theory. recursion goes to the dark with one path. If it does not move, it returns the branch interface and chooses another path, the method of traversing all the paths with multiple branches is similar to the brute force solution of the maze. Each path cannot directly perform interaction operations such as passing values, but we can select a path and select the result that meets our conditions.

Recursion with return values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.