Given a Binary Tree (Every node has at most 2 children) where each node has value either 0 or 1. The task is to convert the given Binary tree to a tree that holds Logical OR property, i.e., each node value should be the logical OR between its children.
Input: 1 / \ 1 0 / \ / \ 0 1 1 1 Output: 0 1 1 1 1 1 1
Explanation: Given Tree 1 / \ 1 0 / \ / \ 0 1 1 1 After Processing 1 / \ 1 1 / \ / \ 0 1 1 1
The idea is to traverse given binary tree in postorder fashion because in postorder traversal both the children of the root has already been visited before the root itself.
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 logic OR of its child data.
Below is the implementation of the above approach:
0 1 1 1 1 1 1
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 AND 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
- 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
- Check whether a binary tree is a full binary tree or not | Iterative Approach
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet