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
- 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 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 a given Binary Tree to Doubly Linked List | Set 1
- 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 2
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
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