#define REP(i, n) for (i = 0; i < n; i++)
typedef struct nodelist node;
typedef struct Queue queue;
queue* createQueue(unsigned capacity)
queue* qu =(queue*)malloc(sizeof(queue));
qu->array = (node **)malloc(qu->capacity * sizeof(node));
int isFull(queue* queue1)
return (queue1->size == queue1->capacity);
int isEmpty(queue* queue1)
return (queue1->size==0);
void enqueue(queue* queue1, node* item)
queue1->rear = (queue1->rear +1 )%queue1->capacity;
queue1->array[queue1->rear] = item;
queue1->size = queue1->size +1;
node dequeue(queue* queue1)
node* item = queue1->array[queue1->front];
queue1->front = (queue1->front +1)%queue1->capacity;
queue1->size = queue1->size -1;
node* front(queue* queue1)
return queue1->array[queue1->front];
node* rear(queue * queue1)
return queue1->array[queue1->rear];
node *createNode(int value)
node *t= (node *) malloc(sizeof(node));
t->right = t->left = NULL;
node *replaceNegativeOne(node *root)
if(root==NULL ||(root->value == -1 && root->left == NULL && root->right == NULL))
root->left = replaceNegativeOne(root->left);
root->right = replaceNegativeOne(root->right);
void deleteTree(node *node1)
deleteTree(node1->right);
void inOrderTraversal(node *t)
inOrderTraversal(t->left);
inOrderTraversal(t->right);
node *createTreeByLevelTree()
queue* queue1 = createQueue(100000);
enqueue(queue1,t->right);
int PlagiarismTest(struct nodelist *root,int x)
if(!root->left && !root->right)
int l= PlagiarismTest(root->left,x);
int r=PlagiarismTest(root->right, x);
return l + r + root->value ;
struct nodelist *root = NULL;
root = createTreeByLevelTree();
root = replaceNegativeOne(root);
int ans=PlagiarismTest(root,x);
#include <stdio.h>
#include<stdlib.h>
#define ll long long
#define REP(i, n) for (i = 0; i < n; i++)
struct nodelist
{
int value;
struct nodelist *left;
struct nodelist *right;
};
typedef struct nodelist node;
struct Queue
{
int front, rear, size;
unsigned capacity;
node* *array;
};
typedef struct Queue queue;
queue* createQueue(unsigned capacity)
{
queue* qu =(queue*)malloc(sizeof(queue));
qu->capacity = capacity;
qu->front = qu->size =0;
qu->rear = capacity-1;
qu->array = (node **)malloc(qu->capacity * sizeof(node));
return qu;
}
int isFull(queue* queue1)
{
return (queue1->size == queue1->capacity);
}
int isEmpty(queue* queue1)
{
return (queue1->size==0);
}
void enqueue(queue* queue1, node* item)
{
if(isFull(queue1))
return ;
queue1->rear = (queue1->rear +1 )%queue1->capacity;
queue1->array[queue1->rear] = item;
queue1->size = queue1->size +1;
}
node dequeue(queue* queue1)
{
node* item = queue1->array[queue1->front];
queue1->front = (queue1->front +1)%queue1->capacity;
queue1->size = queue1->size -1;
return *item;
}
node* front(queue* queue1)
{
return queue1->array[queue1->front];
}
node* rear(queue * queue1)
{
return queue1->array[queue1->rear];
}
node *createNode(int value)
{
node *t= (node *) malloc(sizeof(node));
t->value = value;
t->right = t->left = NULL;
return t;
}
void deleteNode(node*t)
{
free(t);
}
node *replaceNegativeOne(node *root)
{
if(root==NULL ||(root->value == -1 && root->left == NULL && root->right == NULL))
return NULL;
root->left = replaceNegativeOne(root->left);
root->right = replaceNegativeOne(root->right);
return root;
}
void deleteTree(node *node1)
{
if(node1==NULL)
return;
deleteTree(node1->left);
deleteTree(node1->right);
free(node1);
}
void inOrderTraversal(node *t)
{
//write your code here;
if (t == NULL)
return;
inOrderTraversal(t->left);
printf("%d ",t->value);
inOrderTraversal(t->right);
}
node *createTreeByLevelTree()
{
int n,m;
queue* queue1 = createQueue(100000);
node *root, *t;
root = NULL;
while(scanf("%d", &n))
{
if(isEmpty(queue1))
{
root= createNode(n);
enqueue(queue1,root);
continue;
}
scanf("%d", &m);
t = front(queue1);
dequeue(queue1);
t->left =createNode(n);
t->right=createNode(m);
if(t->left->value !=-1)
enqueue(queue1,t->left);
if(t->right->value !=-1)
enqueue(queue1,t->right);
if(isEmpty(queue1))
break;
}
return root;
}
int PlagiarismTest(struct nodelist *root,int x)
{
// write your code here
if(root == NULL) {
return 0;
}
if(root->value==x)
return 0;
if(!root->left && !root->right)
return root->value;
int l= PlagiarismTest(root->left,x);
int r=PlagiarismTest(root->right, x);
return l + r + root->value ;
}
int main() {
struct nodelist *root = NULL;
int x;
scanf("%d", &x);
root = createTreeByLevelTree();
root = replaceNegativeOne(root);
int ans=PlagiarismTest(root,x);
printf("%d",ans);
return 0;
}
#include <stdio.h>
#include<stdlib.h>
#define ll long long
#define REP(i, n) for (i = 0; i < n; i++)
struct nodelist
{
int value;
struct nodelist *left;
struct nodelist *right;
};
typedef struct nodelist node;
struct Queue
{
int front, rear, size;
unsigned capacity;
node* *array;
};
typedef struct Queue queue;
queue* createQueue(unsigned capacity)
{
queue* qu =(queue*)malloc(sizeof(queue));
qu->capacity = capacity;
qu->front = qu->size =0;
qu->rear = capacity-1;
qu->array = (node **)malloc(qu->capacity * sizeof(node));
return qu;
}
int isFull(queue* queue1)
{
return (queue1->size == queue1->capacity);
}
int isEmpty(queue* queue1)
{
return (queue1->size==0);
}
void enqueue(queue* queue1, node* item)
{
if(isFull(queue1))
return ;
queue1->rear = (queue1->rear +1 )%queue1->capacity;
queue1->array[queue1->rear] = item;
queue1->size = queue1->size +1;
}
node dequeue(queue* queue1)
{
node* item = queue1->array[queue1->front];
queue1->front = (queue1->front +1)%queue1->capacity;
queue1->size = queue1->size -1;
return *item;
}
node* front(queue* queue1)
{
return queue1->array[queue1->front];
}
node* rear(queue * queue1)
{
return queue1->array[queue1->rear];
}
node *createNode(int value)
{
node *t= (node *) malloc(sizeof(node));
t->value = value;
t->right = t->left = NULL;
return t;
}
void deleteNode(node*t)
{
free(t);
}
node *replaceNegativeOne(node *root)
{
if(root==NULL ||(root->value == -1 && root->left == NULL && root->right == NULL))
return NULL;
root->left = replaceNegativeOne(root->left);
root->right = replaceNegativeOne(root->right);
return root;
}
void deleteTree(node *node1)
{
if(node1==NULL)
return;
deleteTree(node1->left);
deleteTree(node1->right);
free(node1);
}
void inOrderTraversal(node *t)
{
//write your code here;
if (t == NULL)
return;
inOrderTraversal(t->left);
printf("%d ",t->value);
inOrderTraversal(t->right);
}
node *createTreeByLevelTree()
{
int n,m;
queue* queue1 = createQueue(100000);
node *root, *t;
root = NULL;
while(scanf("%d", &n))
{
if(isEmpty(queue1))
{
root= createNode(n);
enqueue(queue1,root);
continue;
}
scanf("%d", &m);
t = front(queue1);
dequeue(queue1);
t->left =createNode(n);
t->right=createNode(m);
if(t->left->value !=-1)
enqueue(queue1,t->left);
if(t->right->value !=-1)
enqueue(queue1,t->right);
if(isEmpty(queue1))
break;
}
return root;
}
int PlagiarismTest(struct nodelist *root,int x)
{
// write your code here
if(root == NULL) {
return 0;
}
if(root->value==x)
return 0;
if(!root->left && !root->right)
return root->value;
int l= PlagiarismTest(root->left,x);
int r=PlagiarismTest(root->right, x);
return l + r + root->value ;
}
int main() {
struct nodelist *root = NULL;
int x;
scanf("%d", &x);
root = createTreeByLevelTree();
root = replaceNegativeOne(root);
int ans=PlagiarismTest(root,x);
printf("%d",ans);
return 0;
}
#define REP(i, n) for (i = 0; i < n; i++)
#define pb(a) push_back(a)
node *createNode(int value)
t->right = t->left = NULL;
node *replaceNegativeOne(node *root)
if (root == nullptr || (root->value == -1 && root->left == nullptr && root->right == nullptr))
root->left = replaceNegativeOne(root->left);
root->right = replaceNegativeOne(root->right);
node *createTreeByLevelTree()
t->right = createNode(m);
if (t->left->value != -1)
if (t->right->value != -1)
void deleteTree(node *node)
int PlagiarismTest(struct node *root, int x) {
if(root==NULL || root->value == x)
return root->value + PlagiarismTest(root->left,x) + PlagiarismTest(root->right,x);
root = createTreeByLevelTree();
root = replaceNegativeOne(root);
int ans=PlagiarismTest(root, x);
#define REP(i, n) for (i = 0; i < n; i++)
#define pb(a) push_back(a)
#define vi vector<long>
#define ll long long
#include <bits/stdc++.h>
using namespace std;
struct node
{
int value;
node *left;
node *right;
};
node *createNode(int value)
{
node *t = new node();
t->value = value;
t->right = t->left = NULL;
return t;
}
void deleteNode(node *t)
{
delete t;
}
void inorder(node *t)
{
if (t == NULL)
return;
inorder(t->left);
cout << t->value << " ";
inorder(t->right);
}
node *replaceNegativeOne(node *root)
{
if (root == nullptr || (root->value == -1 && root->left == nullptr && root->right == nullptr))
return nullptr;
root->left = replaceNegativeOne(root->left);
root->right = replaceNegativeOne(root->right);
return root;
}
node *createTreeByLevelTree()
{
int n, m;
queue<node *> q;
node *root, *t;
root = nullptr;
while (cin >> n)
{
if (q.empty())
{
root = createNode(n);
q.push(root);
continue;
}
cin >> m;
t = q.front();
q.pop();
t->left = createNode(n);
t->right = createNode(m);
if (t->left->value != -1)
{
q.push(t->left);
}
if (t->right->value != -1)
{
q.push(t->right);
}
if (q.empty())
{
break;
}
}
return root;
}
void deleteTree(node *node)
{
if (node == nullptr)
return;
deleteTree(node->left);
deleteTree(node->right);
delete node;
}
int PlagiarismTest(struct node *root, int x) {
if(root==NULL || root->value == x)
return 0;
return root->value + PlagiarismTest(root->left,x) + PlagiarismTest(root->right,x);
}
int main()
{
node *root = nullptr;
int x;
cin>>x;
root = createTreeByLevelTree();
root = replaceNegativeOne(root);
int ans=PlagiarismTest(root, x);
cout<<ans;
return 0;
}
#define REP(i, n) for (i = 0; i < n; i++)
#define pb(a) push_back(a)
#define vi vector<long>
#define ll long long
#include <bits/stdc++.h>
using namespace std;
struct node
{
int value;
node *left;
node *right;
};
node *createNode(int value)
{
node *t = new node();
t->value = value;
t->right = t->left = NULL;
return t;
}
void deleteNode(node *t)
{
delete t;
}
void inorder(node *t)
{
if (t == NULL)
return;
inorder(t->left);
cout << t->value << " ";
inorder(t->right);
}
node *replaceNegativeOne(node *root)
{
if (root == nullptr || (root->value == -1 && root->left == nullptr && root->right == nullptr))
return nullptr;
root->left = replaceNegativeOne(root->left);
root->right = replaceNegativeOne(root->right);
return root;
}
node *createTreeByLevelTree()
{
int n, m;
queue<node *> q;
node *root, *t;
root = nullptr;
while (cin >> n)
{
if (q.empty())
{
root = createNode(n);
q.push(root);
continue;
}
cin >> m;
t = q.front();
q.pop();
t->left = createNode(n);
t->right = createNode(m);
if (t->left->value != -1)
{
q.push(t->left);
}
if (t->right->value != -1)
{
q.push(t->right);
}
if (q.empty())
{
break;
}
}
return root;
}
void deleteTree(node *node)
{
if (node == nullptr)
return;
deleteTree(node->left);
deleteTree(node->right);
delete node;
}
int PlagiarismTest(struct node *root, int x) {
if(root==NULL || root->value == x)
return 0;
return root->value + PlagiarismTest(root->left,x) + PlagiarismTest(root->right,x);
}
int main()
{
node *root = nullptr;
int x;
cin>>x;
root = createTreeByLevelTree();
root = replaceNegativeOne(root);
int ans=PlagiarismTest(root, x);
cout<<ans;
return 0;
}
Node createNode(int value) {
Node t = new Node(value);
Node replaceNegativeOne(Node root) {
if (root == null || (root.value == -1 && root.left == null && root.right == null)) {
root.left = replaceNegativeOne(root.left);
root.right = replaceNegativeOne(root.right);
Node createTreeByLevelTree(Scanner sc) {
LinkedList<Node> queue = new LinkedList<>();
void deleteTree(Node node) {
static void inOrderTraversal(Node node) {
inOrderTraversal(node.left);
System.out.print(node.value+" ");
inOrderTraversal(node.right);
int PlagiarismTest(Node root, int x)
if(root.left ==null && root.right == null)
int l= PlagiarismTest(root.left,x);
int r=PlagiarismTest(root.right, x);
return l + r + root.value ;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BinaryTree bt = new BinaryTree();
bt.root = bt.createTreeByLevelTree(sc);
bt.root = bt.replaceNegativeOne(bt.root);
int ans=bt.PlagiarismTest(bt.root,x);
import java.util.*;
import java.io.*;
class Node
{
int value;
Node left, right;
public Node(int item)
{
value = item;
left = right = null;
}
}
class BinaryTree {
Node root;
BinaryTree() {
root = null;
}
Node createNode(int value) {
Node t = new Node(value);
return t;
}
Node replaceNegativeOne(Node root) {
if (root == null || (root.value == -1 && root.left == null && root.right == null)) {
return null;
}
root.left = replaceNegativeOne(root.left);
root.right = replaceNegativeOne(root.right);
return root;
}
Node createTreeByLevelTree(Scanner sc) {
int n, m;
LinkedList<Node> queue = new LinkedList<>();
Node t;
root = null;
while (sc.hasNext()) {
n = sc.nextInt();
if (queue.isEmpty()) {
root = createNode(n);
queue.add(root);
continue;
}
m = sc.nextInt();
t = queue.peekFirst();
queue.pop();
t.left = createNode(n);
t.right = createNode(m);
if (t.left.value != -1)
queue.add(t.left);
if (t.right.value != -1)
queue.add(t.right);
if (queue.isEmpty()){
break;
}
}
return root;
}
void deleteTree(Node node) {
node = null;
}
static void inOrderTraversal(Node node) {
//write your code here
if(node == null)
return;
inOrderTraversal(node.left);
System.out.print(node.value+" ");
inOrderTraversal(node.right);
}
int PlagiarismTest(Node root, int x)
{
// write your code here
if(root == null) {
return 0;
}
if(root.value==x)
return 0;
if(root.left ==null && root.right == null)
return root.value;
int l= PlagiarismTest(root.left,x);
int r=PlagiarismTest(root.right, x);
return l + r + root.value ;
}
}
public class Main {
public static void main(String[] args) {
// write your code here
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
BinaryTree bt = new BinaryTree();
bt.root = bt.createTreeByLevelTree(sc);
bt.root = bt.replaceNegativeOne(bt.root);
int ans=bt.PlagiarismTest(bt.root,x);
System.out.println(ans);
bt.deleteTree(bt.root);
}
}
import java.util.*;
import java.io.*;
class Node
{
int value;
Node left, right;
public Node(int item)
{
value = item;
left = right = null;
}
}
class BinaryTree {
Node root;
BinaryTree() {
root = null;
}
Node createNode(int value) {
Node t = new Node(value);
return t;
}
Node replaceNegativeOne(Node root) {
if (root == null || (root.value == -1 && root.left == null && root.right == null)) {
return null;
}
root.left = replaceNegativeOne(root.left);
root.right = replaceNegativeOne(root.right);
return root;
}
Node createTreeByLevelTree(Scanner sc) {
int n, m;
LinkedList<Node> queue = new LinkedList<>();
Node t;
root = null;
while (sc.hasNext()) {
n = sc.nextInt();
if (queue.isEmpty()) {
root = createNode(n);
queue.add(root);
continue;
}
m = sc.nextInt();
t = queue.peekFirst();
queue.pop();
t.left = createNode(n);
t.right = createNode(m);
if (t.left.value != -1)
queue.add(t.left);
if (t.right.value != -1)
queue.add(t.right);
if (queue.isEmpty()){
break;
}
}
return root;
}
void deleteTree(Node node) {
node = null;
}
static void inOrderTraversal(Node node) {
//write your code here
if(node == null)
return;
inOrderTraversal(node.left);
System.out.print(node.value+" ");
inOrderTraversal(node.right);
}
int PlagiarismTest(Node root, int x)
{
// write your code here
if(root == null) {
return 0;
}
if(root.value==x)
return 0;
if(root.left ==null && root.right == null)
return root.value;
int l= PlagiarismTest(root.left,x);
int r=PlagiarismTest(root.right, x);
return l + r + root.value ;
}
}
public class Main {
public static void main(String[] args) {
// write your code here
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
BinaryTree bt = new BinaryTree();
bt.root = bt.createTreeByLevelTree(sc);
bt.root = bt.replaceNegativeOne(bt.root);
int ans=bt.PlagiarismTest(bt.root,x);
System.out.println(ans);
bt.deleteTree(bt.root);
}
}