【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
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
.
解答
用树的递归解决
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int sumNumbers(TreeNode root) {
if(root==null){
return 0;
}
return sum(root,0);
} public int sum(TreeNode node,int parentVal){
int sum=0;
int temp=parentVal*10+node.val;
if(node.left==null&&node.right==null){
sum=temp;
}
if(node.left!=null){
sum+=sum(node.left,temp);
}
if(node.right!=null){
sum+=sum(node.right,temp);
}
return sum;
}
}
---EOF---
【LeetCode】Sum Root to Leaf Numbers的更多相关文章
- 【leetcode】Sum Root to Leaf Numbers(hard)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【Leetcode】【Medium】Sum Root to Leaf Numbers (未完成)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 【树】Sum Root to Leaf Numbers
题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...
- 【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 ...
- [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 ...
- leetcode@ [129] Sum Root to Leaf Numbers (DFS)
https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...
- 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 number ...
- [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 ...
- 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 ...
随机推荐
- ie6,ie7,ie8 css bug汇总以及兼容解决方法
1:li边距“无故”增加 任何事情都是有原因的,li边距也不例外. 先描述一下具体状况:有些时候li边距会突然增 加很多,值也不固定(只在IE6/IE7有这种现象),让人摸不着头脑,仔细“研究”发现是 ...
- WebService之Axis2
写在前面 本文只说Axis2的用法. 1.下载与部署 需要下载两个文件: 下载地址:http://mirrors.cnnic.cn/apache/axis/axis2/java/core/1.7.1/ ...
- C语言入门(11)——switch分支语句
C语言提供了一种用于多分支选择的switch语句, 其一般形式为: switch(表达式) { case 常量表达式1:语句1; break; case 常量表达式2:语句2; break; .... ...
- libcurl get post http
一. 概念 1. 为什么要使用libcurl 1) 作为http的客户端,可以直接用socket连接服务器,然后对到的数据进行http解析,但要 ...
- [Leetcode][Python]36: Valid Sudoku
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- JavaScript常用内置对象(window、document、form对象)
由于刚开始学习B/S编程,下面对各种脚本语言有一个宏观的简单认识. 脚本语言(JavaScript,Vbscript,JScript等)介于HTML和C,C++,Java,C#等编程语言之间.它的优势 ...
- iOS点滴- ViewController详解
一.生命周期 当一个视图控制器被创建,并在屏幕上显示的时候. 代码的执行顺序 1. alloc 创建对象,分配空间 2.init (initW ...
- Nodejs解析HTML网页模块 jsdom
工作需要抓取某些网页,所以今天试用下了node下的jsdom模块.同样功能的还有jquery jsdom https://npmjs.org/package/jsdom API很简单. jsdom.e ...
- Git上手指南
(写在最前:这篇随笔是我在学习git时参考资料+实践出来的,其中一些问题是在实践中遇到的,希望对大家有帮助,还有很多不完整的地方.如果有什么错误的地方欢迎随时向我提出来) 在Git教程之前,我们先来了 ...