-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInput of binary tree.cpp
72 lines (68 loc) · 1.39 KB
/
Input of binary tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* left;
Node* right;
Node(int data){
this->data=data;
this->left=NULL;
this->right=NULL;
}
};
void print(Node* root){
if(root==NULL){
return;
}
print(root->left);
cout<<root->data<<endl;
print(root->right);
}
Node* input(bool isRoot, int parent, bool isLeft){
if(isRoot)
cout<<"Enter root data : ";
else{
if(isLeft)
cout<<"Enter left node of "<<parent<<" : ";
else{
cout<<"Enter right node of "<<parent<<" : ";
}
}
int n;
cin>>n;
if(n == -1)
return NULL;
Node *rootNode = new Node(n);
Node *leftChildofRoot = input(false,n,true);
Node *rightChildOfRoot = input(false,n , false);
rootNode->left = leftChildofRoot;
rootNode->right = rightChildOfRoot;
return rootNode;
}
int countNodes(Node* root){
if(root==NULL){
return 0;
}
int ln=countNodes(root->left);
int rn=countNodes(root->right);
return ln+rn+1;
}
int sumNodes(Node* root){
if(root==NULL){
return 0;
}
int ln=sumNodes(root->left);
int rn=sumNodes(root->right);
return ln+rn+root->data;
}
int main(){
int val,left,right;
// Node* root=input(true,0,false);
Node* root=new Node(1);
root->left=new Node(2);
root->left->left=new Node(4);
print(root);
cout<<"No. of Nodes: "<<countNodes(root)<<endl;
cout<<"Sum of Nodes: "<<sumNodes(root);
}