22、 sum-root-to-leaf-numbers

22、 sum-root-to-leaf-numbers
给定一个仅包含数字0-9的二叉树,每一条从根节点到叶子节点的路径都可以用一个数字表示。
例如根节点到叶子节点的一条路径是1->2->3,那么这条路径就用123来代替。
找出根节点到叶子节点的所有路径表示的数字之和
例如:

1↵ / ↵ 2 3
根节点到叶子节点的路径1->2用数字12代替
根节点到叶子节点的路径1->3用数字13代替
所以答案为12+13=25

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 path1->2->3which represents the number123.
Find the total sum of all root-to-leaf numbers.
For example,
1↵ / ↵ 2 3
The root-to-leaf path1->2represents the number12.
The root-to-leaf path1->3represents the number13.
Return the sum = 12 + 13 =25.

22、 sum-root-to-leaf-numbers

思路:使用树的先序遍历+上一层结点的值*10 + 本层结点的值