Java for LeetCode 129 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

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

解题思路:

递归,JAVA实现如下:

    static public int sumNumbers(TreeNode root) {
    	if(root==null)
    		return 0;
        return sumNumbers(root,0);
    }
    public static int sumNumbers(TreeNode root,int res) {
    	if(root.left==null&&root.right==null)
    		return res*10+root.val;
    	if(root.left==null)
    		return sumNumbers(root.right,res*10+root.val);
    	if(root.right==null)
    		return sumNumbers(root.left,res*10+root.val);
    	return sumNumbers(root.left,res*10+root.val)+sumNumbers(root.right,res*10+root.val);
    }
时间: 2024-10-07 12:31:29

Java for LeetCode 129 Sum Root to Leaf Numbers的相关文章

Leetcode#129 Sum Root to Leaf Numbers

原题地址 二叉树的遍历 代码: 1 vector<int> path; 2 3 int sumNumbers(TreeNode *root) { 4 if (!root) 5 return 0; 6 7 int sum = 0; 8 path.push_back(root->val); 9 10 if (!root->left && !root->right) { 11 for (int i = 0; i < path.size(); i++) 12 s

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

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

LeetCode 129. Sum Root to Leaf Numbers 动态演示

树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: void helper(TreeNode* node, int path, int& sum){ if(!node){ return; } //a(node) //lk("root",node) //a(path) //dsp if(!node->left &&

[LeetCode]129. Sum Root to Leaf Numbers路径数字求和

DFS的标准形式 用一个String记录路径,最后判断到叶子时加到结果上. int res = 0; public int sumNumbers(TreeNode root) { if (root==null) return res; helper(root,""); return res; } public void helper(TreeNode root,String s) { s += root.val+""; if (root.left==null&

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

Leetcode dfs Sum Root to Leaf Numbers

Sum Root to Leaf Numbers Total Accepted: 20237 Total Submissions: 67979My Submissions 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

&lt;LeetCode OJ&gt; 129. Sum Root to Leaf Numbers

Total Accepted: 74843 Total Submissions: 229553 Difficulty: Medium 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

129. Sum Root to Leaf Numbers java solutions

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

[C++]LeetCode: 94 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