LeetCode.1022-根到叶路径二进制数之和(Sum of Root To Leaf Binary Numbers)
这是小川的第381次更新,第410篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第243题(顺位题号是1022)。给定二叉树,每个节点值为0或1.每个根到叶路径表示以最高有效位开始的二进制数。例如,如果路径为0 -> 1 -> 1 -> 0 -> 1,那么这可能表示二进制的01101,即13。
对于树中的所有叶子节点,请考虑从根到该叶子节点的路径所代表的数字。返回这些数字的总和。
例如:
1
/ \
0 1
/ \ / \
0 1 0 1
输入:[1,0,1,0,1,0,1]
输出:22
说明:(100)+(101)+(110)+(111)= 4 + 5 + 6 + 7 = 22
注意:
树中的节点数介于1和1000之间。
node.val是0或1。答案不会超过2^31 - 1。
02 第一种解法
递归的方式解题。
结合题目给的示例来看,可以将该二叉树分为两部分,left和right,left那一支有两条路径100和101,直接做加法就是4+5=9;right那一支也有两条路径110和111,直接做加法就是6+7=13,我们可以将求和拆分成左子树、右子树之和来做。
如果当前节点为空,返回0。如果当前节点如果为叶子节点(没有左子节点和右子节点的节点),就返回此路径所表示的整数。计算路径上的二进制数,可以通用此代码:int val = val*2 + currentNodeValue;,和前天的那道题目在处理累加二进制字符串上类似。
public int sumRootToLeaf(TreeNode root) {
return getSum(root, 0);
}
public int getSum(TreeNode root, int sum) {
if (root == null) {
return 0;
}
// 换成 sum = (sum<<1) + root.val; 效果一样
sum = sum*2 + root.val;
// 当前节点为叶子节点时
if (root.left == null && root.right == null) {
return sum;
}
return getSum(root.left, sum) + getSum(root.right, sum);
}
03 第二种解法
迭代的方式解题。借助两个栈来实现,思路和上面类似。
public int sumRootToLeaf2(TreeNode root) {
if (root == null) {
return 0;
}
int sum = 0;
// 存节点
Stack<TreeNode> stack = new Stack<TreeNode>();
// 存路径所代表整数
Stack<Integer> prevSum = new Stack<Integer>();
stack.push(root);
prevSum.push(root.val);
while (!stack.isEmpty()) {
TreeNode temp = stack.pop();
Integer tempSum = prevSum.pop();
// 左子树
if (temp.left != null) {
stack.push(temp.left);
prevSum.push(tempSum*2 + temp.left.val);
}
// 右子树
if (temp.right != null) {
stack.push(temp.right);
prevSum.push(tempSum*2 + temp.right.val);
}
// 叶子节点,累加完整路径所表示的整数
if (temp.left == null && temp.right == null) {
sum += tempSum;
}
}
return sum;
}
04 小结
算法专题目前已连续日更超过七个月,算法题文章249+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.1022-根到叶路径二进制数之和(Sum of Root To Leaf Binary Numbers)的更多相关文章
- LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)
1022. 从根到叶的二进制数之和 1022. Sum of Root To Leaf Binary Numbers 题目描述 Given a binary tree, each node has v ...
- [Swift]LeetCode1022. 从根到叶的二进制数之和 | Sum of Root To Leaf Binary Numbers
Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary number ...
- 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...
- 1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和
网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 递归调用求和,同时注意%1000000007的位置 /** * ...
- 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
- Leetcode 1022. Sum of Root To Leaf Binary Numbers
dfs class Solution: def sumRootToLeaf(self, root: TreeNode) -> int: stack=[(root,0)] ans=[] bi_st ...
- 【leetcode】1022. Sum of Root To Leaf Binary Numbers
题目如下: Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary n ...
- LeetCode1022. 从根到叶的二进制数之和
题目 class Solution { public: int ans = 0; int sumRootToLeaf(TreeNode* root) { dfs(root,0); return ans ...
- [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 ...
随机推荐
- repo 回退当前分支下所有仓库到指定日期前的最新代码版本
回退命令: repo forall -c 'commitID=git log --before "2019-11-24 23:59" -1 --pretty=format:&quo ...
- qt5--对话框
颜色对话框——QColorDialog: 需要 #include <QColorDialog> QColor color=QColorDialog::getColor(QColor(, ...
- django 做 migrate 时 表已存在的处理方法
django 做 migrate 时 表已存在的处理方法 文章来源:嗨学网 http://www.piaodoo.com 在开发web的时候,如果是以前已存在的项目,项目下载下来后,为了使用测试库的数 ...
- C# WCF发布服务的元数据的方式
发布服务元数据的方式有两种:一是基于HTTP-GET协议提供元数据,它是一种绝大多数平台都能支持的简单text-based协议:另一种是元数据交换终结点. 1.基于HTTP-GET协议 <?xm ...
- javascript / angular 如何把object转成array
取出的api 格式是纯object格式 {"name":"james","city":"Taipei","co ...
- python使用配置文件
通过配置文件将变量暴露给用户修改 标准库模块configparser,从而可在配置文件中使用标准格式. 必须使用[files].[colors]等标题将配置文件分成几部分(section).标题的名称 ...
- 2018 焦作网络赛 L Poor God Water ( AC自动机构造矩阵、BM求线性递推、手动构造矩阵、矩阵快速幂 )
题目链接 题意 : 实际上可以转化一下题意 要求求出用三个不同元素的字符集例如 { 'A' .'B' .'C' } 构造出长度为 n 且不包含 AAA.BBB CCC.ACB BCA.CAC CBC ...
- Linux开机启动和登录时各个文件的执行顺序
1.在Linux内核被加载后,第一个运行的程序便是/sbin/init 该文件会读取/etc/inittab文件,并依据此文件来进行初始化工作.其中/etc/inittab文件最主要的作用就是设定Li ...
- 大哥带的JavaScript伪协议
将javascript代码添加到客户端的方法是把它放置在伪协议说明符javascript:后的URL中.这个特殊的协议类型声明了URL的主体是任意的javascript代码,它由javascript的 ...
- 关于synchronized和ReentrantLock之多线程同步详解
一.线程同步问题的产生及解决方案 问题的产生: Java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查),将会导致数据不准确,相互之间产生冲突. 如下例:假设有一个卖票 ...