Description
Given a binary tree containing digits from 0-9
only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3
which represents the number 123
.
Find the total sum of all root-to-leaf numbers.
Note: A leaf is a node with no children.
Example:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [4,9,0,5,1] |
解法
DFS遍历每一条自顶向下的路径然后累加求和即可~
具体代码如下:
1 | class Solution { |