题目:

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 a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var sumNumbers = function(root) {
if(root==null){
return 0;
} function Node(treeNode,sum){
this.treeNode=treeNode;
this.sum=sum;
} var res=0,stack=[];
var n=new Node(root,root.val);
stack.push(n); while(stack.length!=0){
var p=stack.pop();
if(p.treeNode.left==null&&p.treeNode.right==null){
res+=p.sum;
}else{
if(p.treeNode.left!=null){
stack.push(new Node(p.treeNode.left,p.sum*10+p.treeNode.left.val))
}
if(p.treeNode.right!=null){
stack.push(new Node(p.treeNode.right,p.sum*10+p.treeNode.right.val))
}
}
} return res;
};

【树】Sum Root to Leaf Numbers的更多相关文章

  1. LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II

    1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...

  2. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  3. 23. 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 ...

  4. LeetCode: 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 ...

  5. 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  6. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  7. LeetCode之“树”:Sum Root to Leaf Numbers

    题目链接 题目要求: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represe ...

  8. [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 ...

  9. 129. Sum Root to Leaf Numbers

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

随机推荐

  1. 继承方法-->call继承

    function Person(name,age,sex){ this.name = name; this.age = age; this.sex = sex; }function P1(name,a ...

  2. AD采样的一个例子

    用122.88k时钟采样153.6k的信号

  3. 电信网上营业厅-客户充值缴费时间段数据挖掘--spss

    最近研究分析了“云南电信网上营业厅”e9宽带续约缴费的数据,目前宽带续约量为171人,今天需要谈论的是:如何利用SPSS挖掘出“客户充值缴费的时间段”客户喜欢在哪个时间段来网厅进行充值缴费 云南电信网 ...

  4. 2) 下载Maven

    http://maven.apache.org/ http://maven.apache.org/download.cgi Maven 3.3.3 (Binary tar.gz) Maven 3.3. ...

  5. [翻译]Spring MVC RESTFul Web Service CRUD 例子

    Spring MVC RESTFul Web Service CRUD 例子 本文主要翻译自:http://memorynotfound.com/spring-mvc-restful-web-serv ...

  6. Ubuntu安装教程(双系统)

    经常要重装还不如写个安装教程省的每次都要查 Ubuntu安装教程: win7下安装Linux实现双系统全攻略:https://jingyan.baidu.com/article/c275f6bacc3 ...

  7. bolg迁移

    博客已迁移至:http://www.s0nnet.com 欢迎大家继续关注!!! 2015-7-4

  8. jersey学习笔记

    最近一个项目用到了jersey,我只是负责前端.也借此机会好好了解一下REST webservice及一大推名词. http://redhacker.iteye.com/blog/1914105 1. ...

  9. Android adb shell data目录,Permission denied

    Android adb shell进入data目录,Permission denied 权限被拒绝 在shell里面输入su root,去申请root权限,注意:有小部分手机是需要点击授权,再次执行c ...

  10. TCP/IP协议族分层

    协议族的分层抽象,一定意义上来说,每层敬职敬责的做自己的工作,同时也共同完成通讯协议的共同目标. 这是一个垂直划分的抽象层次,挺有意义. 1.链路层/数据链路层/网络接口层 操作系统中的设备驱动程序和 ...