129. Sum Root to Leaf Numbers
题目描述
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.
|
|
题目大意
根节点到子节点的路径代表一个数,求所有路径的数值的和。
|
|
解题思路
DFS(深度优先搜索)
遍历左右子树,直到左右子树都为nil。则代表这一路径的数值已经组合完毕。
代码
|
|