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]Python中的包(Package)
参考官方文档中的Module和Glosssary中的描述.Module: https://docs.python.org/3/tutorial/modules.html#packagesGlossar ...
- Stream闪亮登场
Stream闪亮登场 一. Stream(流)是什么,干什么 Stream是一类用于替代对集合操作的工具类+Lambda式编程,他可以替代现有的遍历.过滤.求和.求最值.排序.转换等 二. Strea ...
- pywin32模块安装
安装流程: 1.查看python版本和位数: 2.下载对应的的pywin32,下载目录任意 https://sourceforge.net/projects/pywin32/files%2Fpywin ...
- python学习:利用循环语句完善输入设置
利用循环语句完善输入设置 使用for循环: 代码1:_user = "alex"_password = "abc123" for i in range(3): ...
- Golang实现requests库
Golang实现requests库 简单的封装下,方便使用,像python的requests库一样. Github地址 Github 支持 GET.POST.PUT.DELETE applicatio ...
- Centos 搭建邮箱系统
总结 我实操的过程,2个邮箱都没有界面,都只是邮件系统.可能还需要再部署其他东西,暂止. sendmail 比较简单,主要是发邮件,使用 stmp.还需要解决收邮件的问题和管理界面的问题. postf ...
- linux mysql 安装
操作系统 Centos 7.2以上版本 操作系统 centos 7.2以上版本 mysql 版本 mysql-5.7.23-el7-x86_64.tar.gz 1.1 安装准备 1. 创建安装文件存 ...
- win10上使用Xshell通过ssh连接Linux
Windows 10上现在能安装Linux子系统了,正好最近.Net Core也逐渐发展起来了,我也就在自己电脑上搞了一下 在Windows 10上安装Ubuntu的过程就不用说了,都是流程性的东西 ...
- elasticsearch目录
快速入门篇(基于版本5.4) Elasticsearch入门 Elasticsearch和Kibana安装 Elasticsearch索引和文档操作 Elasticsearch文档查询 安装和配置(基 ...
- react-native 组件整理
好早之前整理的部分组件,不全 怕丢