/**************************************************************** 
 * Description: 判断给定的二叉树是否为二叉查找树（BST）
 * Author: Alex Li
 * Date: 2024-08-18 20:28:20
 * LastEditTime: 2024-08-18 20:34:49
****************************************************************/
#include <iostream> 
using namespace std; 

// 定义常量：树的最大节点数量和无穷大值
const int SIZE = 100;
const int INFINITE = 1000000;

// 定义节点结构体，包含左右孩子节点和节点值
struct node
{
    int left_child, right_child, value;
}; 
node a[SIZE]; // 定义节点数组

// 判断以给定根节点为根的子树是否为二叉查找树
// root: 当前子树的根节点编号
// lower_bound: 当前子树的值下界
// upper_bound: 当前子树的值上界
int is_bst(int root, int lower_bound, int upper_bound)
{
    int cur;
    if (root == 0) // 如果当前节点为空（0表示空节点）
        return 1;  // 空节点视为BST，返回1
    
    cur = a[root].value; // 当前节点的值

    // 判断当前节点值是否在合法范围内，并递归检查左右子树
    if ((cur > lower_bound) && (cur < upper_bound) 
        // 左子树：检查左子树的所有节点值是否都小于当前节点值
        && (is_bst(a[root].left_child, lower_bound, cur) == 1) 
        // 右子树：检查右子树的所有节点值是否都大于当前节点值
        && (is_bst(a[root].right_child, cur, upper_bound) == 1))
        return 1; // 如果满足上述条件，则该子树是BST

    return 0; // 否则，不是BST
}

int main() {
    int i, n; 
    cin >> n; // 输入节点数

    // 输入每个节点的值以及左右子节点编号
    for (i = 1; i <= n; i++)
        cin >> a[i].value >> a[i].left_child >> a[i].right_child;
    
    // 输出根节点为1的树是否为BST（1表示是，0表示否）
    cout << is_bst(1, -INFINITE, INFINITE) << endl;
    
    return 0;
}