Naive Approach: The idea is to generate all the root to leaf path and check whether all nodes in every path is even or not. Count all the paths with even nodes in it and return the count. The above implementation takes extra space to store the path.
- If current value of the node is odd or pointer becomes NULL then return the count.
- If the current node is a leaf node then increment the count by 1.
- Recursively call for the left and right subtree with the updated count.
- After all recursive call, the value of count is number of even paths for a given binary tree.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the number of nodes in the given binary tree.
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.
- Count all k-sum paths in a Binary Tree
- Count of Fibonacci paths in a Binary tree
- Count of exponential paths in a Binary Tree
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Count of root to leaf paths in a Binary Tree that form an AP
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Print all k-sum paths in a binary tree
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Print all the paths from root, with a specified sum in Binary tree
- Print Palindromic Paths of Binary tree
- Given a binary tree, print all root-to-leaf paths
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Root to leaf paths having equal lengths in a Binary Tree
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Count the Number of Binary Search Trees present in a Binary Tree
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Count of subtrees in a Binary Tree having XOR value K
- Count of 1's in any path in a Binary Tree
- Count pairs in a binary tree whose sum is equal to a given value x
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.