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
- 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 4
- 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 1
- Convert a given Binary Tree to Doubly Linked List | Set 2
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- 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
- Convert a given Binary Tree to Circular Doubly Linked List | Set 2
- 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 email@example.com. 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