树——sum-root-to-leaf-numbers(根到叶节点数字之和)

问题

  Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.

  An example is the root-to-leaf path1->2->3which represents the number123.

  Find the total sum of all root-to-leaf numbers.

  For example,

      1
     /     2   3

  The root-to-leaf path1->2represents the number12.
  The root-to-leaf path1->3represents the number13.

  Return the sum = 12 + 13 =25.

代码

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public int sumNumbers(TreeNode root) {
12        int sum = 0;
13        if(root==null)
14            return sum;
15        return sumNumbers(root,sum);
16     }
17
18     public int sumNumbers(TreeNode root, int sum){
19         if(root==null)
20             return 0;
21         sum = sum*10+root.val;
22         if(root.left==null && root.right==null)
23             return sum;
24         return sumNumbers(root.left, sum) + sumNumbers(root.right, sum);
25     }
26 }
时间: 2024-10-27 21:47:23

树——sum-root-to-leaf-numbers(根到叶节点数字之和)的相关文章

[LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和

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. For example, 1 / 2 3 T

[LeetCode] 129 Sum Root to Leaf Numbers 求根到叶节点数字之和

此题题意是求所有从根结点到叶节点的路径转化为数字后之和. 因为是二叉树,容易想到是用递归求解. 整体思想是从根到叶子进行遍历,其中不断保存当前的中间结果(上一层的结果乘以10后加上当前层根节点的数值)并通过参数向下传递... 到达叶子节点时可以逐层返回最终结果.解法不难,但有几个细节需要考虑清楚. 1)可以用递归函数dfs的参数表内置的返回和来返回数值,也可以直接用dfs返回值,对应于以下两种定义: void dfs(TreeNode *root, int &sum, int pre); int

LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)

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. For example, 1 / 2 3 T

【leetcode刷题笔记】Sum Root to Leaf Numbers

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. For example, 1 / 2 3 T

Sum Root to Leaf Numbers

Sum Root to Leaf Numbers 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 number

[leetcode]Sum Root to Leaf Numbers @ Python

原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: 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. Fin

Sum Root to Leaf Numbers leetcode java

题目: 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. For example, 1 / 2

LeetCode: Sum Root to Leaf Numbers [129]

[题目] 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. For example, 1 /

LeetCode——Sum Root to Leaf Numbers

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. For example, 1 / 2 3 T