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 email@example.com. 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
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Minimum swap required to convert binary tree to binary search tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert a Binary Tree into its Mirror Tree
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Check whether a given binary tree is skewed binary tree or not?
- Convert a given Binary Tree to Doubly Linked List | Set 1
- Convert a given Binary Tree to Doubly Linked List | Set 2
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Convert a given Binary Tree to Doubly Linked List | Set 4
- Convert a given Binary Tree to Circular Doubly Linked List | Set 2
- Difference between Binary Tree and Binary Search Tree
- Convert a given tree to its Sum Tree
- Check if a binary tree is subtree of another binary tree | Set 1
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 2
- Check whether a binary tree is a full binary tree or not