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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count all k-sum paths in a Binary Tree
- Count of exponential paths in a Binary Tree
- Count of Fibonacci 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 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 1's in any path in a Binary Tree
- Count of subtrees in a Binary Tree having XOR value K
- Count of subtrees in a Binary Tree having bitwise OR value K
- Count Non-Leaf nodes 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.