
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert a given Binary tree to a tree that holds Logical AND property on C++
In this tutorial, we will be discussing a program to convert a given Binary tree to a tree that holds Logical AND property.
For this we will be provided with a binary tree. Our task is to convert it into a tree that holds the logical AND property means that a node has a value of the AND operation of its children nodes. Note that every node can have a value either zero or one.
Example
#include<bits/stdc++.h> using namespace std; //node structure of binary tree struct Node{ int data; struct Node* left; struct Node* right; }; //creation of a new node struct Node* newNode(int key){ struct Node* node = new Node; node->data= key; node->left = node->right = NULL; return node; } //converting the tree with nodes following //logical AND operation void transform_tree(Node *root){ if (root == NULL) return; //moving to first left node transform_tree(root->left); //moving to first right node transform_tree(root->right); if (root->left != NULL && root->right != NULL) root->data = (root->left->data) & (root->right->data); } //printing the inorder traversal void print_tree(Node* root){ if (root == NULL) return; print_tree(root->left); printf("%d ", root->data); print_tree(root->right); } int main(){ Node *root=newNode(0); root->left=newNode(1); root->right=newNode(0); root->left->left=newNode(0); root->left->right=newNode(1); root->right->left=newNode(1); root->right->right=newNode(1); printf("Before conversion :\n"); print_tree(root); transform_tree(root); printf("\nAfter conversion :\n"); print_tree(root); return 0; }
Output
Before conversion : 0 1 1 0 1 0 1 After conversion : 0 0 1 0 1 1 1
- Related Articles
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property in C++
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue) in C++
- Convert a Binary Tree into its Mirror Tree in C++
- Python program to convert a given binary tree to doubly linked list
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- Convert a given Binary Tree to Doubly Linked List (Set 1) in C++
- Convert a given Binary Tree to Doubly Linked List (Set 2) in C++
- Convert Ternary Expression to a Binary Tree in C++
- Create a mirror tree from the given binary tree in C++ Program
- Program to pruning a given binary tree in C++
- Difference between Binary Tree and Binary Search Tree
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- Convert a Binary Tree to a Circular Doubly Link List in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++

Advertisements