-
Notifications
You must be signed in to change notification settings - Fork 9
/
BSTNodeinRange.cpp
58 lines (52 loc) · 1.06 KB
/
BSTNodeinRange.cpp
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
/*
BST nodes in a range
Given a binary search tree of integers. You are given a range [B, C]. Return the count of the number of nodes that lies in this range. Constraints
1 <= Number of nodes in binary tree <= 100000
0 <= B < = C <= 10^9
For Example
Input 1:
15
/ \
12 20
/ \ / \
10 14 16 27
/
8
B = 12
C = 20
Output 1:
5
Input 2:
8
/ \
6 21
/ \
1 4
B = 2
C = 20
Output 2:
3
*/
// Do any traversal and check if element in range
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
int inRange(TreeNode *A, int B, int C)
{
if (A == NULL)
return 0;
int ans = 0;
if (A->val >= B && A->val <= C)
ans++;
return ans + inRange(A->left, B, C) + inRange(A->right, B, C);
}
int Solution::solve(TreeNode *A, int B, int C)
{
return inRange(A, B, C);
}