CODE 4: Sum Root to Leaf Numbers

原创 2013年09月09日 22:47:01

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 number123.

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.

 

My Code:

	private int sum;

	public int sumNumbers(TreeNode root) {
		// Start typing your Java solution below
		// DO NOT write main() function
		this.sum = 0;
		dfs(root, 0);
		return this.sum;
	}

	private void dfs(TreeNode root, int val) {
		if (null == root) {
			return;
		}
		if (null == root.left && null == root.right) {
			this.sum += root.val + val * 10;
		}
		if (null != root.left) {
			dfs(root.left, val * 10 + root.val);
		}
		if (null != root.right) {
			dfs(root.right, val * 10 + root.val);
		}

	}


 

Sum Root to Leaf Numbers -- LeetCode

原题链接: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/  这是一道树的题目,一般使用递归来做,主要就是考虑递归条件和结束条件。...
  • linhuanmars
  • linhuanmars
  • 2014-04-04 09:20:44
  • 8908

leetcode Sum Root to Leaf Numbers 详细思考分析过程

做这道题的时候,我自己遇到的问题: 1. 如何分情况? 2. 如何递归到该点的时候该如何操作? 3. 由跟节点到叶子节点的时候,每进入一层就需要把前面的值*10,如何处理这个值?总值又是如何保存的...
  • kenden23
  • kenden23
  • 2013-11-03 09:15:06
  • 8922

LeetCode(129)SumRoottoLeafNumbers

题目如下: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent ...
  • feliciafay
  • feliciafay
  • 2014-02-28 15:57:49
  • 1344

【leetcode】129. Sum Root to Leaf Numbers【java】

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numb...
  • cslbupt
  • cslbupt
  • 2017-01-16 11:16:53
  • 121

【LeetCode】Sum Root to Leaf Numbers 解题报告

【题目】 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could repres...
  • ljiabin
  • ljiabin
  • 2014-09-22 17:14:49
  • 2970

【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(所有根到叶子结点组组成的数字相加)】

【129-Sum Root to Leaf Numbers(所有根到叶子结点组组成的数字相加)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-15 06:11:18
  • 1917

Sum Root to Leaf Numbers 根节点到叶子组成数的和

Sum Root to Leaf Numbers   Given a binary tree containing digits from 0-9 only, each root-to-lea...
  • u012605629
  • u012605629
  • 2015-05-07 18:08:30
  • 303

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 numb...
  • geekmanong
  • geekmanong
  • 2015-12-31 14:30:09
  • 1537

sum root to leaf numbers

题目描述Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a numb...
  • coder_lj
  • coder_lj
  • 2018-03-21 13:17:19
  • 4

Sum Root to Leaf Numbers

public int sumNumbers(TreeNode root) { // Start typing your Java solution below // D...
  • qiqiqi777
  • qiqiqi777
  • 2013-08-18 10:06:51
  • 279
收藏助手
不良信息举报
您举报文章:CODE 4: Sum Root to Leaf Numbers
举报原因:
原因补充:

(最多只允许输入30个字)