-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_0998_insertIntoMaxTree.cc
95 lines (82 loc) · 2.34 KB
/
Problem_0998_insertIntoMaxTree.cc
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include "UnitTest.h"
using namespace std;
class TreeNode
{
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution
{
public:
TreeNode *process(TreeNode *cur, int val)
{
if (cur == nullptr)
{
return new TreeNode(val, nullptr, nullptr);
}
if (cur->val < val)
{
TreeNode *node = new TreeNode(val, cur, nullptr);
return node;
}
cur->right = process(cur->right, val);
return cur;
}
TreeNode *insertIntoMaxTree(TreeNode *root, int val) { return process(root, val); }
};
bool isSameValueStructure(TreeNode *head1, TreeNode *head2)
{
if (head1 == nullptr && head2 != nullptr)
{
return false;
}
if (head1 != nullptr && head2 == nullptr)
{
return false;
}
if (head1 == nullptr && head2 == nullptr)
{
return true;
}
if (head1->val != head2->val)
{
return false;
}
return isSameValueStructure(head1->left, head2->left) && isSameValueStructure(head1->right, head2->right);
}
void testInsertIntoMaxTree()
{
Solution s;
TreeNode *x1 = new TreeNode(1, nullptr, nullptr);
TreeNode *x2 = new TreeNode(2, nullptr, nullptr);
TreeNode *x3 = new TreeNode(3, x2, nullptr);
TreeNode *x4 = new TreeNode(4, x1, x3);
TreeNode *x5 = new TreeNode(5, x4, nullptr);
TreeNode *y1 = new TreeNode(1, nullptr, nullptr);
TreeNode *y2 = new TreeNode(2, nullptr, y1);
TreeNode *y3 = new TreeNode(3, nullptr, nullptr);
TreeNode *y4 = new TreeNode(4, nullptr, nullptr);
TreeNode *y5 = new TreeNode(5, y2, y4);
TreeNode *y44 = new TreeNode(4, nullptr, y3);
TreeNode *y55 = new TreeNode(5, y2, y44);
TreeNode *z1 = new TreeNode(1, nullptr, nullptr);
TreeNode *z2 = new TreeNode(2, nullptr, z1);
TreeNode *z3 = new TreeNode(3, nullptr, nullptr);
TreeNode *z4 = new TreeNode(4, z3, nullptr);
TreeNode *z5 = new TreeNode(5, z2, z3);
TreeNode *z55 = new TreeNode(5, z2, z4);
EXPECT_TRUE(isSameValueStructure(x5, s.insertIntoMaxTree(x4, 5)));
EXPECT_TRUE(isSameValueStructure(y55, s.insertIntoMaxTree(y5, 3)));
EXPECT_TRUE(isSameValueStructure(z55, s.insertIntoMaxTree(z5, 4)));
EXPECT_SUMMARY;
}
int main()
{
testInsertIntoMaxTree();
};