Leetcode 129. Sum Root to Leaf Numbers

来源:互联网 发布:美术生送什么礼物 知乎 编辑:程序博客网 时间:2024/06/08 10:16
public class Solution {    public int sumNumbers(TreeNode root) {        return helper(root, 0);    }        public static int helper(TreeNode r, int tmp) {        if (r == null) return 0;                // current node is a leaf        if (r.left == null && r.right == null) {            return tmp + r.val;        }                // current node is not a leaf        return helper(r.left, (tmp+r.val)*10) + helper(r.right, (tmp+r.val)*10);    }}

0 0
原创粉丝点击