Given a Binary Tree (Every node has at most 2 children) where each node has value either 0 or 1. Convert a given Binary tree to a tree that holds Logical AND property, i.e., each node value should be the logical AND between its children.
Input : The below tree doesn’t hold the logical AND property convert it to a tree that holds the property. 1 / \ 1 0 / \ / \ 0 1 1 1 Output : 0 / \ 0 1 / \ / \ 0 1 1 1
The idea is to traverse given binary tree in postorder fashion. For each node check (recursively) if the node has one children then we don’t have any need to check else if the node has both its child then simply update the node data with the logical AND of its child data.
Inorder traversal before conversion 0 1 1 0 1 0 1 Inorder traversal after conversion 0 0 1 0 1 1 1
This article is contributed by Roshni Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Convert a given Binary tree to a tree that holds Logical OR property
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Minimum swap required to convert binary tree to binary search tree
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Convert a Binary Tree into its Mirror Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert left-right representation of a binary tree to down-right
- Convert Ternary Expression to a Binary Tree
- Convert a given Binary Tree to Doubly Linked List | Set 1
- Convert a given Binary Tree to Doubly Linked List | Set 2
- Convert ternary expression to Binary Tree using Stack
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Convert a given Binary Tree to Doubly Linked List | Set 4
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Convert a given Binary Tree to Circular Doubly Linked List | Set 2
- Convert a Binary Tree to a Circular Doubly Link List
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Check whether a binary tree is a full binary tree or not | Iterative Approach