304163: CF797D. Broken BST

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Broken BST

题意翻译

``` **题意:** 给一棵二叉搜索树,但是不保证这是一棵正确的二叉搜索树,那么按照二叉搜索树的搜索算法(小往左,大往右),可能找不到某些节点,你的任务是计算有多少节点将不会被遍历到. **输入:** 第一行一个整数n(1<=n<=100000)表示节点数目 接下来n行每行三个整数v(0<=v<=10^9),l,r,分别表示节点值,左孩子和右孩子的编号,注意若该子节点不存在用-1表示,不同节点的值可能相同 **输出:** 找不到的节点有多少个 ``` 贡献者:凌幽

题目描述

Let $ T $ be arbitrary binary tree — tree, every vertex of which has no more than two children. Given tree is rooted, so there exists only one vertex which doesn't have a parent — it's the root of a tree. Every vertex has an integer number written on it. Following algorithm is run on every value from the tree $ T $ : 1. Set pointer to the root of a tree. 2. Return success if the value in the current vertex is equal to the number you are looking for 3. Go to the left child of the vertex if the value in the current vertex is greater than the number you are looking for 4. Go to the right child of the vertex if the value in the current vertex is less than the number you are looking for 5. Return fail if you try to go to the vertex that doesn't exist Here is the pseudo-code of the described algorithm: `<br></br>bool find(TreeNode t, int x) {<br></br> if (t == null)<br></br> return false;<br></br> if (t.value == x)<br></br> return true;<br></br> if (x < t.value)<br></br> return find(t.left, x);<br></br> else<br></br> return find(t.right, x);<br></br>}<br></br>find(root, x);<br></br>`The described algorithm works correctly if the tree is binary search tree (i.e. for each node the values of left subtree are less than the value in the node, the values of right subtree are greater than the value in the node). But it can return invalid result if tree is not a binary search tree. Since the given tree is not necessarily a binary search tree, not all numbers can be found this way. Your task is to calculate, how many times the search will fail being running on every value from the tree. If the tree has multiple vertices with the same values on them then you should run algorithm on every one of them separately.

输入输出格式

输入格式


First line contains integer number $ n $ ( $ 1<=n<=10^{5} $ ) — number of vertices in the tree. Each of the next $ n $ lines contains $ 3 $ numbers $ v $ , $ l $ , $ r $ ( $ 0<=v<=10^{9} $ ) — value on current vertex, index of the left child of the vertex and index of the right child of the vertex, respectively. If some child doesn't exist then number $ -1 $ is set instead. Note that different vertices of the tree may contain the same values.

输出格式


Print number of times when search algorithm will fail.

输入输出样例

输入样例 #1

3
15 -1 -1
10 1 3
5 -1 -1

输出样例 #1

2

输入样例 #2

8
6 2 3
3 4 5
12 6 7
1 -1 8
4 -1 -1
5 -1 -1
14 -1 -1
2 -1 -1

输出样例 #2

1

说明

In the example the root of the tree in vertex $ 2 $ . Search of numbers $ 5 $ and $ 15 $ will return fail because on the first step algorithm will choose the subtree which doesn't contain numbers you are looking for.

Input

题意翻译

``` **题意:** 给一棵二叉搜索树,但是不保证这是一棵正确的二叉搜索树,那么按照二叉搜索树的搜索算法(小往左,大往右),可能找不到某些节点,你的任务是计算有多少节点将不会被遍历到. **输入:** 第一行一个整数n(1<=n<=100000)表示节点数目 接下来n行每行三个整数v(0<=v<=10^9),l,r,分别表示节点值,左孩子和右孩子的编号,注意若该子节点不存在用-1表示,不同节点的值可能相同 **输出:** 找不到的节点有多少个 ``` 贡献者:凌幽

加入题单

上一题 下一题 算法标签: