import
java.util.*;
class
Node {
int
val;
Node left;
Node right;
Node(
int
value, Node l, Node r)
{
val = value;
left = l;
right = r;
}
Node(
int
value) {
this
(value,
null
,
null
); }
}
class
Stack {
ArrayList<Integer> stack =
new
ArrayList<>();
int
size() {
return
stack.size(); }
int
top()
{
return
(size() >
0
) ? stack.get(size() -
1
) :
0
;
}
void
push(
int
val) { stack.add(val); }
void
pop()
{
if
(size() >=
1
) {
stack.remove(size() -
1
);
}
else
{
stack.clear();
}
}
void
rolling_push(
int
val,
int
period)
{
int
to_remove_idx = size() - period;
int
to_remove = (to_remove_idx <
0
)
?
0
: stack.get(to_remove_idx);
int
to_add = (to_remove_idx <=
0
)
?
0
: stack.get(to_remove_idx -
1
);
if
(size() <=
0
) {
push(val);
}
else
{
push(val + stack.get(size() -
1
) - to_remove
+ to_add);
}
}
void
show()
{
for
(
int
item : stack) {
System.out.print(item +
" "
);
}
System.out.println();
}
}
public
class
Main {
static
int
count =
0
;
static
Stack s =
new
Stack();
static
void
count_with_greater_sum(Node root_node,
int
x)
{
if
(root_node ==
null
) {
return
;
}
s.rolling_push(root_node.val,
3
);
if
(s.size() >=
3
&& s.top() > x) {
count++;
}
count_with_greater_sum(root_node.left, x);
count_with_greater_sum(root_node.right, x);
s.pop();
}
public
static
void
main(String[] args)
{
Node root =
new
Node(
10
);
root.left =
new
Node(
1
);
root.right =
new
Node(
22
);
root.left.left =
new
Node(
35
);
root.left.right =
new
Node(
4
);
root.right.left =
new
Node(
15
);
root.right.right =
new
Node(
67
);
root.left.left.left =
new
Node(
57
);
root.left.left.right =
new
Node(
38
);
root.left.right.left =
new
Node(
9
);
root.left.right.right =
new
Node(
10
);
root.right.left.left =
new
Node(
110
);
root.right.left.right =
new
Node(
312
);
root.right.right.left =
new
Node(
131
);
root.right.right.right =
new
Node(
414
);
root.right.left.right.left =
new
Node(
8
);
root.right.right.right.right =
new
Node(
39
);
count_with_greater_sum(root,
100
);
System.out.println(count);
}
}