xxxxxxxxxx
void preorder(node *n) {
cout << n->value;
if(n->left!=NULL)
preorder(n->left);
if (n->right != NULL)
preorder(n->right);
}
void inorder(node *n) {
if (n->left != NULL)
inorder(n->left);
cout << n->value;
if (n->right != NULL)
inorder(n->right);
}
void postorder(node *n) {
if (n->left != NULL)
postorder(n->left);
if (n->right != NULL)
postorder(n->right);
cout << n->value;
}
xxxxxxxxxx
class Node:
def __init__(self,key):
self.left = None
self.right = None
self.val = key
root = Node(1)
root.left = Node(2);
root.right = Node(3);
root.left.left = Node(4);
xxxxxxxxxx
#include <iostream>
#include <queue>
using namespace std;
struct Node{
int data;
struct Node* left, *right;
};
// Function to count the full Nodes in a binary tree
int fullcount(struct Node* node){
// Check if tree is empty
if (!node){
return 0;
}
queue<Node *> myqueue;
// traverse using level order traversing
int result = 0;
myqueue.push(node);
while (!myqueue.empty()){
struct Node *temp = myqueue.front();
myqueue.pop();
if (temp->left && temp->right){
result++;
}
if (temp->left != NULL){
myqueue.push(temp->left);
}
if (temp->right != NULL){
myqueue.push(temp->right);
}
}
return result;
}
struct Node* newNode(int data){
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
int main(void){
struct Node *root = newNode(10);
root->left = newNode(20);
root->right = newNode(30);
root->left->left = newNode(40);
root->left->right = newNode(50);
root->left->left->right = newNode(60);
root->left->right->right = newNode(70);
cout <<"count is: "<<fullcount(root);
return 0;
}
xxxxxxxxxx
void preorder(node *n) {
cout << n->value;
if(n->left!=NULL)
preorder(n->left);
if (n->right != NULL)
preorder(n->right);
}
void inorder(node *n) {
if (n->left != NULL)
inorder(n->left);
cout << n->value;
if (n->right != NULL)
inorder(n->right);
}
void postorder(node *n) {
if (n->left != NULL)
postorder(n->left);
if (n->right != NULL)
postorder(n->right);
cout << n->value;
}
xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
// Val is the key or the value that
// has to be added to the data part
Node(int val)
{
data = val;
// Left and right child for node
// will be initialized to null
left = NULL;
right = NULL;
}
};
int main()
{
/*create root*/
Node* root = new Node(1);
/* following is the tree after above statement
1
/ \
NULL NULL
*/
root->left = new Node(2);
root->right = new Node(3);
/* 2 and 3 become left and right children of 1
1
/ \
2 3
/ \ / \
NULL NULL NULL NULL
*/
root->left->left = new Node(4);
/* 4 becomes left child of 2
1
/ \
2 3
/ \ / \
4 NULL NULL NULL
/ \
NULL NULL
*/
return 0;
}