LeetCode 求根到叶子节点数字之和
class Solution { int result; public int sumNumbers(TreeNode root) { if(root == null)return 0; sumNumber(root, 0); return result; } public void sumNumber(TreeNode root , int sum) { int temp = sum*10 + root.val; if(root == null)return; if(root.left == null && root.right == null) //达到叶节点 { result += temp; return; } if(root.left != null) { sumNumber(root.left , temp); } if(root.right != null) { sumNumber(root.right , temp); } } }