Root to leaf path sum
Given a Binary Tree and a sum s, your task is to check whether there is a root to leaf path in that tree with the following sum.
Algorithm
- Traverse tree from root to leaf
- If node is null then return false
- If its leaf and sum – leaf.data == 0 then return true
- Recursively call left child wilt sum – node.data
- Recursively call right child with sum – node.data
Latest Source Code:
Github: RootTLeafPathSumChecker.java
Output:
10 / \ / \ 8 2 / \ / 3 5 2 Sum = 23 ? true Sum = 18 ? false Sum = 12 ? false Sum = 14 ? true Sum = 10 ? false