import java.util.Stack;

/**
*
* Source : https://oj.leetcode.com/problems/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.
*
*/
public class SumRootToLeafNumbers { /**
* 求出由根节点到叶子节点组成所有数字的和
*
* 可以使用深度优先DFS求出所有的数字然后求和
* 也可以使用BFS,逐层求和,求和的时候不是直接加该节点的值,是该节点的值加上上一节点的10倍(子节点处表示的数字就是root.value*10 + node.value)
* 直到最后一个没有子节点的节点的时候,该节点的值就是最后的和
*
* 相当于将根节点到叶子节点路径所表示的数字集中到叶子节点上,然后对叶子节点求和
*
* @param root
* @return
*/
public int sum (TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
int sum = 0; while (stack.size() > 0) {
TreeNode node = stack.pop();
if (node.leftChild != null) {
node.leftChild.value += node.value * 10;
stack.push(node.leftChild);
}
if (node.rightChild != null) {
node.rightChild.value += node.value * 10;
stack.push(node.rightChild);
}
if (node.leftChild == null && node.rightChild == null) {
sum += node.value;
}
}
return sum;
} public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
} private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value; public TreeNode(int value) {
this.value = value;
} public TreeNode() { }
} public static void main(String[] args) {
SumRootToLeafNumbers sumRootToLeafNUmbers = new SumRootToLeafNumbers();
char[] arr = new char[]{'1','2','3'};
System.out.println(sumRootToLeafNUmbers.sum(sumRootToLeafNUmbers.createTree(arr)));
} }

leetcode — sum-root-to-leaf-numbers的更多相关文章

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

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

  3. [leetcode]Sum Root to Leaf Numbers @ Python

    原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...

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

  5. LeetCode: Sum Root to Leaf Numbers [129]

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

  6. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

    Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...

  7. LeetCode :: Sum Root to Leaf Numbers [tree、dfs]

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

  8. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

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

  9. LeetCode Sum Root to Leaf Numbers(DFS)

    题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...

  10. leetcode Sum Root to Leaf Numbers(所有路径之和)

    转载请注明来自souldak,微博:@evagle 观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的.那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最 ...

随机推荐

  1. jquery for循环

    第一种: for(var i=0,len=$len.length; i<len; i++){//alert($len.eq(i).html());$zongshu=$zongshu+$len.e ...

  2. 删除PeopleSoft Process Scheduler服务器定义

    DELETE FROM PS_SERVERDEFN WHERE SERVERNAME= 'PSNT2' ; DELETE FROM PSSERVERSTAT where SERVERNAME = 'P ...

  3. Vue中scoped css和css module比较

    scoped css 官方文档 scoped css可以直接在能跑起来的vue项目中使用. 使用方法: <style scoped> h1 { color: #f00; } </st ...

  4. GeoHash(Java实现)

    package com.koubei.collect_script.demo; import java.util.ArrayList; import java.util.Arrays; import ...

  5. js 事件模型详解

    把js的事件模型,分为两类,DOM0级和DOM2级, DOM0级 通常直接在DOM对象上绑定函数对象,指定事件类型,dom.onClick = function(){};类似于这种写法,移除事件,则直 ...

  6. 机器学习(九)隐马尔可夫模型HMM

    1.隐马尔可夫HMM模型 一个隐马尔可夫模型可以表示为\[\lambda=\{A,B,\pi\}\]具体就不说了,比较基本. 2.HMM模型的三个基本问题 1.概率计算问题:给定\(\lambda\) ...

  7. mysql数据库内容相关操作

    第一:介绍 mysql数据内容的操作主要是: INSERT实现数据的插入 UPDATE实现数据的更新 DLETE实现数据的删除 SELECT实现数据的查询. 第二:增(insert) 1.插入完整的数 ...

  8. 用 PHP文件引入css样式

    html: <link rel="stylesheet" type="text/css" href="http://www.mysite.com ...

  9. kibana 创建index pattern 索引模式时过慢导致无法创建成功 以及解决方案

    下面我具体描述一下我遇到的问题. 在kibana上面创建索引点击创建时,一直显示下面的页面 就看到不停的在那转,始终创建不成功. 查看后台日志,看到状态码为403,报了如下的错误 由于我用的是es6版 ...

  10. Expedition---POJ - 2431

    A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poo ...