赞
踩
题目:
题解:
- type pair struct {
- node *TreeNode
- num int
- }
-
- func sumNumbers(root *TreeNode) (sum int) {
- if root == nil {
- return
- }
- queue := []pair{{root, root.Val}}
- for len(queue) > 0 {
- p := queue[0]
- queue = queue[1:]
- left, right, num := p.node.Left, p.node.Right, p.num
- if left == nil && right == nil {
- sum += num
- } else {
- if left != nil {
- queue = append(queue, pair{left, num*10 + left.Val})
- }
- if right != nil {
- queue = append(queue, pair{right, num*10 + right.Val})
- }
- }
- }
- return
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。