import java.util.*;
public class BinaryTree {
static Scanner scn = new Scanner(System.in);
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
System.out.println(bt.isBalanced());
}
private class Node {
int data;
Node left;
Node right;
}
private Node root;
private int size;
public BinaryTree() {
this.root = this.takeInput(null, false);
}
public Node takeInput(Node parent, boolean ilc) {
int cdata = scn.nextInt();
Node child = new Node();
child.data = cdata;
this.size++;
// left
boolean hlc = scn.nextBoolean();
if (hlc) {
child.left = this.takeInput(child, true);
}
// right
boolean hrc = scn.nextBoolean();
if (hrc) {
child.right = this.takeInput(child, false);
}
return child;
}
public int height() {
return this.height(this.root);
}
private int height(Node node) {
if(node==null) {
return -1;}
int lheight= this.height(node.left);
int rheight=this.height(node.right);
int height= Math.max(lheight, rheight)+1;
return height;
}
public boolean isBalanced() {
return this.isBalanced(this.root);
}
private boolean isBalanced(Node node) {
// write your code here
int lh;
int rh;
if (node == null)
return true;
lh = height(node.left); //left tree height
rh = height(node.right); //right tree height
if (Math.abs(lh - rh )<= 1
&& isBalanced(node.left)
&& isBalanced(node.right))
return true;
return false;
}