-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathBinaryTreePruning.cpp
More file actions
38 lines (34 loc) · 1.18 KB
/
Copy pathBinaryTreePruning.cpp
File metadata and controls
38 lines (34 loc) · 1.18 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
/**
* Definition for a binary tree node.
* struct TreeNode {
* 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 *pruneTree(TreeNode *root)
{
return containsOne(root) ? root : NULL;
}
// To check whether the root node contains 1 and if not it will prune (remove) that node
bool containsOne(TreeNode *root)
{
if (root == NULL)
return false;
bool left_subtree = containsOne(root->left);
bool right_subtree = containsOne(root->right)
if (!left_subtree)
root->left = NULL;
if (!right_subtree)
root->right = NULL;
return left_subtree || right_subtree || root->val == 1;
}
};
// Time Complexity - O(n) -> n is the number of nodes in the binary tree
// Space Complexity - O(h) - where h is the height of the binary tree and the size of implicit recurisive call stack