-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathTrim_a_binary_tree.cpp
More file actions
44 lines (38 loc) · 1.45 KB
/
Trim_a_binary_tree.cpp
File metadata and controls
44 lines (38 loc) · 1.45 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
/**
* 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* trimBST(TreeNode* root, int low, int high) {
//there are 3 cases that arise
//case 1: root is NULL
if(!root)return root;
//case 2: root value lies within the range
if(root->val >=low && root->val <=high) //both included
{
//then find its left and right subtree
root->left = trimBST(root->left ,low,high);
root->right = trimBST(root->right,low,high);
return root;
}
//case 3: root value lies outside the range
if(root->val<low)
{
//then check for right subtree of root because there is a possibility
//that the right subtree lies in the range
//but there is no chance in hell that the left subree lies in the range
//as the root itself doesn't lies and the left is going to be even smaller
return trimBST(root->right,low,high);
}
return trimBST(root->left,low,high);
//else check for left if root->val is greater than upper range
}
};