近况
最近实在太忙了,上周末还加了两天班,这两天利用晚上下班时间(并不是正常下班, 你懂的),把二叉树相关的算法大概敲了一遍,找找感觉。
代码
直接上代码:
1 |
|
测试代码: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
61
62
63
64
using namespace std;
int main()
{
// binary tree
TreeNode* root;
Solution solution;
cout << "create tree begin!" << endl;
solution.CreateTree(root); // create
cout << endl;
cout << "create tree end!" << endl;
cout << "preTraverse tree begin!" << endl;
solution.preTraverse(root); // 前序
cout << endl;
cout << "no recursive preTraverse tree begin!" << endl;
solution.preTraverseNoRec(root); // 非递归前序
cout << endl;
cout << "midTraverse tree begin!" << endl;
solution.midTraverse(root); // 中序
cout << endl;
cout << "no recursive midTraverse tree begin!" << endl;
solution.midTraverseNoRec(root); // 非递归中序
cout << endl;
cout << "postTraverse tree begin!" << endl;
solution.postTraverse(root); // 后序
cout << endl;
cout << "no recursive postTraverse tree begin!" << endl;
solution.postTraverseNoRec(root); // 非递归后序
cout << endl;
cout << "levelTraverse tree begin!" << endl;
solution.levelTraverse(root); // 层次
cout << endl;
cout << "binary tree node numbers: "<< solution.BinaryTreeNodeNumbers(root) << endl;
cout << "binary tree depth: "<< solution.BinaryTreeDepth(root) << endl;
// 输出二叉树 1.2 层节点个数
cout << "binary tree 1 level size: " << solution.BinaryTreeKLevelSize(root, 1) << endl;
cout << "binary tree 2 level size: " << solution.BinaryTreeKLevelSize(root, 2) << endl;
cout << "binary tree is valid BST: " << solution.isValidBST(root) << endl;
cout << "binary tree is valid BST no rec: " << solution.isValidBSTNoRec(root) << endl;
cout << "binary tree is valid AVL: " << solution.isAVLTree(root) << endl;
cout << "binary tree test end!" << endl;
solution.DestoryTree(root);
return 0;
}
以前序遍历 1->2->3->2->4->5 为例,过一遍用例,打印结果如下: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[root@yejy binaryTree]# valgrind ./a.out
==18094== Memcheck, a memory error detector
==18094== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==18094== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==18094== Command: ./a.out
==18094==
create tree begin!
1
2
3
0
0
2
0
0
4
5
0
0
0
create tree end!
preTraverse tree begin!
1 2 3 2 4 5
no recursive preTraverse tree begin!
1 2 3 2 4 5
midTraverse tree begin!
3 2 2 1 5 4
no recursive midTraverse tree begin!
3 2 2 1 5 4
postTraverse tree begin!
3 2 2 5 4 1
no recursive postTraverse tree begin!
3 2 2 5 4 1
levelTraverse tree begin!
1 2 4 3 2 5
binary tree node numbers: 6
binary tree depth: 3
binary tree 1 level size: 2
binary tree 2 level size: 3
binary tree is valid BST: 0
binary tree is valid BST no rec: 0
binary tree is valid AVL: 1
binary tree test end!
==18094==
==18094== HEAP SUMMARY:
==18094== in use at exit: 0 bytes in 0 blocks
==18094== total heap usage: 26 allocs, 26 frees, 5,904 bytes allocated
==18094==
==18094== All heap blocks were freed -- no leaks are possible
==18094==
==18094== For counts of detected and suppressed errors, rerun with: -v
==18094== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
大概就这样,晚安!!