Path Sum*
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and
sum = 22
Solution:
asdfasdf首先我开始用了分治的思想,其实这道题不需要使用,分治算法的本质是把大问题划分成小问题,然后一路向下到最末级,然后把结果bubble up上来,这道题不需要,直接可以递归下去,把数值一路减下去,特殊的地方就是,需要知道等于0的那个节点,root.left == none and root.right == none
Last updated
Was this helpful?