How powerful recursion is, just look at the problem.
With this problem, you can master
- How to use recursion
- The nature of recursion
- How to jump out of a recursive loop
Title: Enter an integer and a two-dollar tree.
A path is formed from the root node of the tree down to all the nodes that pass through to the leaf node.
Prints out all paths equal to the input integers.
For example, enter 20 and two fork trees as follows
Print out path 10 6 4
Ideas
- When accessing a node, add the node to the path and accumulate the value of the current node.
- If the current node is a leaf node and the current path is exactly equal to the input integer, then the current path meets the requirements and we print it out.
- If the current node is not a leaf node, continue to access its child nodes.
For the sub-nodes, we will find that the action to be performed is the same as the 3.1 modules above us! This is implying that you should use recursion!
As we traverse, we push each node into the stack. This requires us to let the node out of the stack after the traversal, or the function will never jump out.
So we want to delete the current node on the path and subtract the value of the current node before the function exits to ensure that the path is exactly the root node to the parent node when the parent node is returned.
Source
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <sstream> #include < Vector>using namespace std;/** finds and all paths for a value in a binary tree title: Enter an integer and a binary tree. A path is formed from the root node of the tree down to all the nodes that pass through to the leaf node. Print out and all paths equal to input integers when you access a node, add the node to the path and accumulate the value of the current node. If the current node is a leaf node and the current path is exactly equal to the input integer, then the current path meets the requirements and we print it out. If the current node is not a leaf node, continue to access its child nodes. So we want to delete the current node on the path and subtract the value of the current node before the function exits to ensure that the path is exactly the root node to the parent node when the parent node is returned. It is not difficult to see that the data structure of a saved path is actually a stack structure, because the path is consistent with the recursive invocation state, and the recursive invocation is essentially a process of compacting and stacking. */struct Binarytreenode//A node in the binary tree{int m_nvalue;//value of Nodebinarytreenode *m_pleft; of Nodebinarytreenode *m_pright; Right child of node};//sum equals the path of a value void Findpath (Binarytreenode * node,int expectadd,vector<int> path,int sum) {if (null==node)//node is empty return;path.push_back (node->m_nvalue); sum+=node->m_nvalue;// If the current node is a leaf node and the current path is exactly equal to the input integer,//Then the current path meets the requirements and we print it out. if (null==node->m_pleft&&null==node->m_pright&&sum==expectadd) {cout<< "Find path:" < <endl;for (int i =0;i<path. Size (); i++) {cout<<path[i]<< "";} Cout<<endl;} If the current node is not a leaf node, continue to access its child nodes. if (null!=node->m_pleft) Findpath (node->m_pleft,expectadd,path,sum), if (null!=node->m_pright) FindPath ( Node->m_pright,expectadd,path,sum);//To delete the current node on the path before the function exits and subtract the value of the current node//to ensure that the path is exactly the path of the root node to the parent node when the parent node is returned. Path.pop_back (); sum-=node->m_nvalue;} void Main () {//Generate two fork tree, use a rather foolish practice here, you can design Binarytreenode * root=new binarytreenode (); root->m_pleft=new Binarytreenode (); root->m_pright=new Binarytreenode (); root->m_pleft->m_pleft=new BinaryTreeNode (); root- >m_pright->m_pright=new Binarytreenode (); root->m_nvalue=10;root->m_pleft->m_nvalue=6;root->m_ Pright->m_nvalue=2;root->m_pleft->m_pleft->m_nvalue=4;root->m_pright->m_pright->m_nvalue= 7;//set parameter int expectadd = 20;vector<int> path;int sum=0;//look for Path Findpath (root,expectadd,path,sum); System ("Pause") ;}
Run
For recursion, remember that a recursive invocation is essentially a process of stacking and stacking.
C Language Enhancement (iv) Two-fork tree path summing to a value