-
Notifications
You must be signed in to change notification settings - Fork 0
/
inorderTraversal_by_recursive.cpp
60 lines (50 loc) · 1.19 KB
/
inorderTraversal_by_recursive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
// 定义二叉树节点
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
// 中序遍历函数
void inorderTraversal(TreeNode *root)
{
if (root == nullptr)
return;
// 遍历左子树
inorderTraversal(root->left);
// 访问根节点
std::cout << root->val << " ";
// 遍历右子树
inorderTraversal(root->right);
}
int main()
{
// 构建示例二叉树
TreeNode *root = new TreeNode(4);
root->left = new TreeNode(2);
root->right = new TreeNode(6);
root->left->left = new TreeNode(1);
root->left->right = new TreeNode(3);
root->right->left = new TreeNode(5);
root->right->right = new TreeNode(7);
/*
* 4
/ \
2 6
/ \ / \
1 3 5 7
*/
// 进行中序遍历
inorderTraversal(root);
// 释放内存(实际使用中可以考虑使用智能指针自动管理内存)
delete root->left->left;
delete root->left->right;
delete root->right->left;
delete root->right->right;
delete root->left;
delete root->right;
delete root;
return 0;
}