---
# System prepended metadata

title: 0129. Sum Root to Leaf Numbers
tags: [Leetcode, FaceBook, Tree Traversal, Medium]

---

# 0129. Sum Root to Leaf Numbers
###### tags: `Leetcode` `Medium` `FaceBook` `Tree Traversal`
Link: https://leetcode.com/problems/sum-root-to-leaf-numbers/
## Code
```java=
class Solution {
    class NumNode{
        TreeNode node;
        int num;
        NumNode(TreeNode node, int num){
            this.node = node;
            this.num = num;
        }
    }
    public int sumNumbers(TreeNode root) {
        Stack<NumNode> stack = new Stack();
        stack.push(new NumNode(root, root.val));
        int res = 0;
        while(!stack.isEmpty()){
            NumNode nnode = stack.pop();
            if(nnode.node.left!=null){
                stack.push(new NumNode(nnode.node.left,nnode.num*10+nnode.node.left.val));
            }
            if(nnode.node.right!=null){
                stack.push(new NumNode(nnode.node.right,nnode.num*10+nnode.node.right.val));
            }
            if(nnode.node.right==null && nnode.node.left==null){
                res+=nnode.num;
            }
        }
        return res;
    }
}
```