-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum_tree.cpp
More file actions
141 lines (121 loc) · 3.74 KB
/
Sum_tree.cpp
File metadata and controls
141 lines (121 loc) · 3.74 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <utility>
using namespace std;
// Sum Tree
// link - https://www.geeksforgeeks.org/problems/sum-tree/1
/*
Given a Binary Tree. Check for the Sum Tree for every node except the leaf node. Return true if it is a Sum Tree otherwise, return false.
A SumTree is a Binary Tree where the value of a node is equal to the sum of the nodes present in its left subtree and right subtree. An empty tree is also a Sum Tree as the sum of an empty tree can be considered to be 0. A leaf node is also considered a Sum Tree.
Examples:
Input:
3
/ \
1 2
Output: true
Explanation: The sum of left subtree and right subtree is 1 + 2 = 3, which is the value of the root node. Therefore,the given binary tree is a sum tree.
Input:
10
/ \
20 30
/ \
10 10
Output: false
Explanation: The given tree is not a sum tree. For the root node, sum of elements in left subtree is 40 and sum of elements in right subtree is 30. Root element = 10 which is not equal to 30+40.
Input:
25
/ \
9 15
Output: false
Constraints:
2 ≤ number of nodes ≤ 105
1 ≤ node->data ≤ 105
*/
/* Tree node */
struct Node
{
int data;
Node *left, *right;
Node() : data(0), left(nullptr), right(nullptr) {}
Node(int x) : data(x), left(nullptr), right(nullptr) {}
Node(int x, Node *left, Node *right) : data(x), left(left), right(right) {}
};
// Time Complexity: O(N) where N is the number of nodes in the tree
// Space Complexity: O(H) where H is the height of the tree (recursion stack)
class Solution
{
pair<bool, int> isValid(Node *root)
{
if (!root)
return {true, 0};
if (!root->left && !root->right)
return {true, root->data};
pair<bool, int> l = isValid(root->left);
pair<bool, int> r = isValid(root->right);
return {(l.first && r.first) && (l.second + r.second) == root->data, l.second + r.second + root->data};
}
public:
bool isSumTree(Node *root)
{
if (!root)
return true;
return isValid(root).first;
}
};
// Main function to test the solution
int main()
{
Solution sol;
// Test case 1:
// 3
// / \
// 1 2
// Expected: true
Node *root1 = new Node(3);
root1->left = new Node(1);
root1->right = new Node(2);
cout << "Test case 1:" << endl;
cout << " 3" << endl;
cout << " / \\" << endl;
cout << " 1 2" << endl;
cout << "Result: " << (sol.isSumTree(root1) ? "true" : "false") << endl;
// Test case 2:
// 10
// / \
// 20 30
// / \
// 10 10
// Expected: false
Node *root2 = new Node(10);
root2->left = new Node(20);
root2->right = new Node(30);
root2->left->left = new Node(10);
root2->left->right = new Node(10);
cout << "\nTest case 2:" << endl;
cout << " 10" << endl;
cout << " / \\" << endl;
cout << " 20 30" << endl;
cout << " / \\" << endl;
cout << " 10 10" << endl;
cout << "Result: " << (sol.isSumTree(root2) ? "true" : "false") << endl;
// Test case 3:
// 25
// / \
// 9 15
// Expected: false
Node *root3 = new Node(25);
root3->left = new Node(9);
root3->right = new Node(15);
cout << "\nTest case 3:" << endl;
cout << " 25" << endl;
cout << " / \\" << endl;
cout << " 9 15" << endl;
cout << "Result: " << (sol.isSumTree(root3) ? "true" : "false") << endl;
// Test case 4: Empty tree - should return true
cout << "\nTest case 4: Empty tree" << endl;
cout << "Result: " << (sol.isSumTree(nullptr) ? "true" : "false") << endl;
// Test case 5: Single node - should return true
Node *root5 = new Node(5);
cout << "\nTest case 5: Single node (5)" << endl;
cout << "Result: " << (sol.isSumTree(root5) ? "true" : "false") << endl;
return 0;
}