标签:color col return class .com span bsp vat 自己
129 Sum Root to Leaf Numbers https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/41363/Short-Java-solution.-Recursion. 不是自己写的, tree 的 recursion 做的 不好 class Solution { public int sumNumbers(TreeNode root) { return sum(root, 0); } private int sum(TreeNode n, int prev){ if(n == null){ return 0; } if(n.right == null && n.left == null){ return prev * 10 + n.val; } int left = sum(n.left, prev * 10 + n.val); int right = sum(n.right, prev * 10 + n.val); int sum = left + right; return sum; } }
标签:color col return class .com span bsp vat 自己
原文地址:https://www.cnblogs.com/tobeabetterpig/p/9454828.html