二叉树的简单认识与应用
认识递归序
递归一棵树的时候,如果完全递归结束,每个结点可以回到3次;
- 打印递归序code
#include<iostream>
#include<vector>
using namespace std;
struct Node{
Node * leftson;
Node* rightson;
int val;
Node(int n)
{
val = n;
leftson = nullptr;
rightson = nullptr;
};
};
void get_value(Node* root)
{
Node* cur = root;
if (cur == NULL)
return;
cout << cur->val << " ";
get_value(cur->leftson);
cout << cur->val << " ";
get_value(cur->rightson);
cout << cur->val << " ";
}
int main()
{
Node* root = new Node(1);
root->leftson = new Node(2);
root->rightson = new Node(3);
root->leftson->leftson = new Node(4);
root->leftson->rightson = new Node(5);
root->rightson->leftson = new Node(6);
root->rightson->rightson = new Node(7);
get_value(root);
return 0;
}
- 递归序结果
1,2,4,4,4
2,5,5,5
2,1,3
6,6,6,3,7,7,7,3,1