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遍历这棵树,然后到叶子节点的时候将值加到最 ...
随机推荐
- 用户注册之后,通过网易邮箱服务器(smtp.163.com)发送电子邮箱到注册者邮箱的的确认通知短信.(可根据需求自行调整)
Member 是数据实体,穿过来的也就是当前注册用户的信息. 存储的数据一定要有邮箱信息 private void SendAuthCodeToMember(Member member) ...
- VS之设置文件编码格式
VS2012默认格式为 "GB2312-80",很多时候可能出现乱码情况,就是编码问题,如何在VS里修改呢? 文件->“高级保存选项 ” 选择gb2312
- 非vue-cli的花括号闪现问题
<div id="app" v-cloak></div>[v-cloak] { display: none;}这种方式可以解决网速较慢,vue.js文件还没 ...
- Centos服务器上NFS灾备环境及KVM的搭建及使用
1.概述 由于在单台服务器上搭建灾备环境需要KVM和NFS的支持,下面先列出KVM的搭建流程,再列出使用NFS实现单台服务器灾备的流程. A.搭建KVM环境 1>.主机环境准备 Linux Sy ...
- Vue 过滤器的使用
Vue官方文档是这样说的:Vue过滤器用于格式化一些常见的文本. 在实际项目中的使用: 定义过滤器 在src定义一个filter.js文件,里面定义过滤器函数,在最后要使用 exprot defaul ...
- ckeditor+ckfinder
官方地址:http://ckeditor.com/ 复制ckeditor和ckfinder的文件夹到项目根路径下 拷贝ckfinder的config.xml到WEB-INF下 <config&g ...
- hive独立配置mysql
版本 安装好jdk,hadoop配置好环境变量 配置 HIVE_HOME 开始安装hive, 在 /home/hadoop/apache-hive-1.2.1-bin/conf 创建文件 hive-s ...
- js-day06-jQuery事件和DOM操作-练习题
jQuery事件绑定 js中绑定事件,三种方式: 方式1: 直接在元素上,增加onXxx事件属性. <button onclick="alert(1);">点我< ...
- QEMU KVM Libvirt手册(7): 硬件虚拟化
在openstack中,如果我们启动一个虚拟机,我们会看到非常复杂的参数 qemu-system-x86_64 -enable-kvm -name instance-00000024 -S -mach ...
- go 语言的序列化与反序列化
与c 语言一样, 在网络编程中, go语言同样需要进行序列化与反序列化 在c语言中, 通常需要一块内存缓冲区用来收 发数据.缓冲区一般定义成char *buff类型. 当需要发送 数据时, 直接使用m ...