题目:

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. Linux 系统中进程5中常见状态

    运行.中断.不可终端.僵死.停止 R(运行):正在运行 or 在运行队列中等待: S(中断):处于休眠中,等待接收信号,并脱离改状态: D(不可中断):不响应信号输入,即使kill也不起作用: Z(僵 ...

  2. (KMP 水)Wow! Such Doge! -- hdu -- 4847

    http://acm.hdu.edu.cn/showproblem.php?pid=4847 Wow! Such Doge! Time Limit:1000MS     Memory Limit:32 ...

  3. MFC自动生成代码详解(一)

    首先声明这篇博客是给MFC刚刚上路的coder准备的,老鸟们就自觉无视我吧! 大家有没有感觉,创建MFC工程时他总会生成一大堆文件一大堆代码.虽然给我们带来了便利,但是调试的时候碰到这些代码总是畏首畏 ...

  4. python 的几种启动方式

    python 的几种启动方式 (1)利用Win的操作系统的:命令行工具 cmd.exe Win + R  调出运行对话框,然后输入cmd,即可调出“命令提示符对话框” 或者 在菜单中店家附件中的命令提 ...

  5. Oracle EBS 常用网站列表

    http://www.lifandong.com/erp/356 Oracle EBS 常用网站列表 英文站: Oracle EBS R12 官方参考手册(User Guide, Implementa ...

  6. django drf 动态权限配置和动态seriaizer_class配置

    可以通过重写get_permissions方法和get_serializers_class方法来实现动态的配置权限和序列化 VIewDemo class RegUserSet(mixins.Creat ...

  7. 双缓冲队列解决WPF界面卡死

    工作中的项目,CS客户端会通过MQ接收前端设备发送的信息,之前测试的时候,由于测试的数据不大,没有进行压力测试,软件可以正常工作,随着项目现场设备数量的增加,CS客户端从MQ中订阅的数据量不断增加,最 ...

  8. 嵌入的资源 和 Resource

    我们将资源文件添加至.net C#工程时,文件的生成操作有多种可选方式.通常用的多的是两种:[嵌入的资源]和[Resource],如果从需要从代码中使用这些资源文件,不同生成操作则对应不同的引用方式: ...

  9. error while loading shared libraries: libudev.so.0 的问题

    在linux下运行koala的时候遇到了问题: 应该是缺少文件libudev.so.0 在/lib和/lib64目录下也都没有这个文件.然后网上找解决方案,在/lib/x86_64-linux-gnu ...

  10. Uliweb之 ORM基本使用(Sqlalchemy)

    参考:http://limodou.github.io/uliweb-doc/zh_CN/db/orm.html#title_1-2 ORM基本使用¶ 使用要求¶ 需要安装sqlalchemy 0.7 ...