题目:
Given a binary tree containing digits from 0-9
only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3
which represents the number 123
.
Find the total sum of all root-to-leaf numbers.
Note: A leaf is a node with no children.
Example:
Input: [1,2,3] 1 / \ 2 3 Output: 25 Explanation: The root-to-leaf path1->2
represents the number12
. The root-to-leaf path1->3
represents the number13
. Therefore, sum = 12 + 13 =25
.
Example 2:
Input: [4,9,0,5,1] 4 / \ 9 0 / \ 5 1 Output: 1026 Explanation: The root-to-leaf path4->9->5
represents the number 495. The root-to-leaf path4->9->1
represents the number 491. The root-to-leaf path4->0
represents the number 40. Therefore, sum = 495 + 491 + 40 =1026
.
前面正好做了257打印出所有路径,这道题就只是把这些打印出来的路径变成int value然后相加就行了。直接这么做了先。
Runtime: 1 ms, faster than 29.35% of Java online submissions for Sum Root to Leaf Numbers.
Memory Usage: 36.6 MB, less than 16.49% of Java online submissions for Sum Root to Leaf Numbers.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int sumNumbers(TreeNode root) {
List<String> paths = binaryTreePaths(root);
int result = 0;
for (String s : paths) {
result += Integer.valueOf(s);
}
return result;
}
private List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
if (root == null) {
return result;
}
helper(root, new ArrayList<>(), result);
return result;
}
public void helper(TreeNode root, List<Integer> temp, List<String> result) {
temp.add(root.val);
if (root.left == null && root.right == null) {
StringBuilder sb = new StringBuilder();
for (int i : temp) {
sb.append(i);
}
result.add(sb.toString());
return;
}
if (root.left != null) {
helper(root.left, temp, result);
temp.remove(temp.size() - 1);
}
if (root.right != null) {
helper(root.right, temp, result);
temp.remove(temp.size() - 1);
}
}
}
然后自己又重新写了个递归版本的,不需要像上面那个那样add又remove了(盲猜是因为之前的是pass by reference所以temp会被更新,这里是pass by value所以不用管?),但是更新result要把result写成global variable才能真正被更新到。
Runtime: 0 ms, faster than 100.00% of Java online submissions for Sum Root to Leaf Numbers.
Memory Usage: 36.5 MB, less than 16.49% of Java online submissions for Sum Root to Leaf Numbers.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int result = 0;
public int sumNumbers(TreeNode root) {
if (root == null) {
return result;
}
helper(root, 0);
return result;
}
private void helper(TreeNode root, int temp) {
temp = temp * 10 + root.val;
if (root.left == null && root.right == null) {
result += temp;
}
if (root.left != null) {
helper(root.left, temp);
}
if (root.right != null) {
helper(root.right, temp);
}
}
}
看了discussion,其实还可以直接让helper return int,想起来稍微有点费劲,但写起来是真的优雅。
Runtime: 0 ms, faster than 100.00% of Java online submissions for Sum Root to Leaf Numbers.
Memory Usage: 36.9 MB, less than 16.49% of Java online submissions for Sum Root to Leaf Numbers.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int sumNumbers(TreeNode root) {
return helper(root, 0);
}
private int helper(TreeNode root, int temp) {
if (root == null) {
return 0;
}
temp = temp * 10 + root.val;
if (root.left == null && root.right == null) {
return temp;
}
return helper(root.left, temp) + helper(root.right, temp);
}
}