Given a binary tree and a value K, the task is to check if every node of the binary tree has either value of the node as K or at least one of its adjacent connected nodes has value K.
Input: 1 / \ 0 0 / \ \ 1 0 1 / / \ \ 2 1 0 5 / / 3 0 / 0 K = 0 Output: False Explanation: We can observe that some leaf nodes are having value other than 0 and are not connected with any adjacent node whose value is 0. Input: 0 / \ 2 1 \ 0 K = 0 Output: True Explanation: Since, all nodes of the tree are either having value 0 or connected with adjacent node having value 0.
- The idea is to simply perform preorder traversal and pass reference of parent node recursively.
- While traversing for each node check the following conditions:
- if value of the node is K or,
- if its parent node value is K or,
- if any of its child node value is K.
- If any node of the tree doesn’t satisfy any of the given three conditions then, return False, else return True.
Below is the implementation of the above approach:
Time complexity: O(N), N is the number of nodes of the tree.
Auxiliary Space: O(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.
- Node having maximum sum of immediate children and itself in n-ary tree
- Count of nodes in a Binary tree with immediate children as its factors
- Count nodes having highest value in the path from root to itself in a Binary Tree
- Count nodes having smallest value in the path from root to itself in a Binary Tree
- Count of nodes in a Binary Tree whose immediate children are co-prime
- Count of Nodes whose both immediate children are its prime factors
- 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
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check whether a given binary tree is skewed binary tree or not?
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Number of unique paths in tree such that every path has a value greater than K
- Order of teams in a tournament such that every team has won against its consecutive team
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Immediate Smaller element in an N-ary Tree
- Node having maximum number of nodes less than its value in its subtree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Symmetric Tree (Mirror Image of itself)
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Check if a Binary Tree (not BST) has duplicate values
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.