/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcsumNumbers(root*TreeNode)int{ifroot==nil{return0}sum:=0vardfsfunc(root*TreeNode,numint)dfs=func(root*TreeNode,numint){num=num*10+root.Valifroot.Left==nil&&root.Right==nil{sum+=numreturn}ifroot.Left!=nil{dfs(root.Left,num)}ifroot.Right!=nil{dfs(root.Right,num)}}dfs(root,0)returnsum}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcsumNumbers(root*TreeNode)int{sum:=0ifroot==nil{returnsum}typeqElemstruct{node*TreeNodenumint}varqueue[]qElemqueue=append(queue,qElem{root,root.Val})forlen(queue)>0{cur:=queue[0]queue=queue[1:]ifcur.node.Left==nil&&cur.node.Right==nil{sum+=cur.num}ifcur.node.Left!=nil{queue=append(queue,qElem{cur.node.Left,cur.num*10+cur.node.Left.Val})}ifcur.node.Right!=nil{queue=append(queue,qElem{cur.node.Right,cur.num*10+cur.node.Right.Val})}}returnsum}