题目:

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. Navicat如何导出数据库的svg、pdf,png图片

    有时候各位可能有这么一种感觉,如果一个数据库中的表太多的话,查看起来不大方便,如果你习惯用navicat软件来查看er图的话,那也是更困难了,这里介绍一种方法,就是把这些关系结构导出一个可以用浏览器打 ...

  2. 高博-《视觉SLAM十四讲》

    0 讲座 (1)SLAM定义 对比雷达传感器和视觉传感器的优缺点(主要介绍视觉SLAM) 单目:不知道尺度信息 双目:知道尺度信息,但测量范围根据预定的基线相关 RGBD:知道深度信息,但是深度信息对 ...

  3. C++ 中的运算符的优先级

    C++ 中的运算符的优先级 直接看图

  4. POJ1511来回最短路

    POJ1511 问你从1到其它点得所有最短路之和  与  其他点到1得所有最短路之和 得总和 思路很明确就是两次最短路,翻转一次地图就好了 一开始就是两次spfa之间处理好数据得更新管理就好 vect ...

  5. D3 API总览

    D3图形库API参考 https://github.com/d3/d3/wiki/API--%E4%B8%AD%E6%96%87%E6%89%8B%E5%86%8C d3 官网 API https:/ ...

  6. delphi 手机振动 IOS Android

    delphi 手机振动 IOS Android delphi  手机振动 IOS Android 振动 https://community.embarcadero.com/blogs/entry/ho ...

  7. [翻译]NUnit---TearDown and SetUpFixture and Test Attributes(二十)

    TearDownAttribute (NUnit 2.0 / 2.5) 本特性在TestFixture内部使用,每个测试方法执行后调用的方法集.也可以在SetUpFixture中使用,在同一命名空间或 ...

  8. supervisor配置环境变量(PATH)

    app配置中增加: environment=PATH="/PATH/TO/anaconda3/bin" supervisord在linux中启动默认继承了linux的环境变量,在这 ...

  9. 前台ajax请求一次,后台代码执行了两次

    问题: 后台使用Django,前台发布文章时使用ajax方式,后台执行代码将data数据取出存入数据库 经过排查,是提交按钮type写成了submit,需要写成button,真是 修改后测试 代码执行 ...

  10. SignalR简单封装

    需求:Asp.Net MVC 开发客户端,实现与服务器端实时通信. 众所周知,Web开发是基于http的请求响应模型,每次刷新都需要客户端(浏览器)主动发起请求,那么,这个问题怎么解?Asp.Net ...