leetcode — sum-root-to-leaf-numbers
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的更多相关文章
- 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 ...
- [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]Sum Root to Leaf Numbers @ Python
原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...
- 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: 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 ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- 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 ...
- [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 ...
- LeetCode Sum Root to Leaf Numbers(DFS)
题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...
- leetcode Sum Root to Leaf Numbers(所有路径之和)
转载请注明来自souldak,微博:@evagle 观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的.那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最 ...
随机推荐
- python学习笔记(6)
第6章 组合数据类型 组合类型的三种表达形式:集合.序列.字典 集合类型及操作 定义:集合是多个元素的无序组合 集合类型与数学中的集合概念一致 集合元素之间无序,每个元素唯一,不存在相同元素 集合元素 ...
- FTP连接超时
今天程序在连接FTP服务器,突然无法连接,用Windows 的 Explorer能正常连接,但用 WebRequest.WebResponse连接时,总是抛出连接超时异常. 后查找相关资料,原因是:程 ...
- JS与IOS、Android的交互
一.JS与Android 放在了assets文件夹下了(注意若使用的是AS这个IDE,assets文件夹应放在src/main目录下) <!DOCTYPE html> <html&g ...
- mysql 水平分表
新建10张表,user_0,user_1,...user_9,方法不可串用,采用hash或取余法,获取要操作的表名,取值用对应存值的方法 1.hash取余法 public function part_ ...
- SpringSecurity实现权限管理和页面导航栏动态实现
用户模块. 3 1.1 需求:获取用户名. 3 1.1.1 分析. 3 1.1.2 服务端获取用户信息. 4 1.1.3 页面获取用户信息. 5 1.2 给用户分配角色. ...
- SQL笛卡尔积查询与关联查询性能对比
首先声明一下,sql会用略懂,不是专家,以下内容均为工作经验,聊以抒情. 今天帮忙验证同事发布的端口时,查看了一下相关sql内容,发现其使用的sql语句会导致笛卡尔积现象,为了帮其讲解进行了如下分析: ...
- [error] - Build path is incomplete. Cannot find class file for org/aspectj/weaver/refl
将本地仓库中mybatis 的jar 包删除,然后在eclipse 中右键工程选中 Maven->upgrade ..
- Live2D插件--漂浮的二次元小姐姐
这个插件找了很久,都没找到,今天偶然翻到一个小哥的博客发现了这个,果断偷走. 教程转自简书:https://www.jianshu.com/p/1cedcf183633 还有这些,你可能有用 修改位置 ...
- vue-router下的html5 history在iis服务器上的设置 vue去掉#
转自:https://www.cnblogs.com/zzsdream/p/6576639.html 1.安装 url rewrite模块到IIS 下载地址 2.在web.config文件中 syst ...
- SpringMVC的配置和使用
SpringMVC的配置和使用 什么是SpringMVC? SpringMVC是Spring家族的一员,Spring是将现在开发中流行的组件进行组合而成的一个框架!它用在基于MVC的表现层开发,类似于 ...