# 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; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up