这是小川的第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 第一种解法

递归的方式解题。

结合题目给的示例来看,可以将该二叉树分为两部分,leftrightleft那一支有两条路径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)的更多相关文章

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

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

  3. 【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 ...

  4. 1022. Sum of Root To Leaf Binary Numbers从根到叶的二进制数之和

    网址:https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/ 递归调用求和,同时注意%1000000007的位置 /** * ...

  5. 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...

  6. Leetcode 1022. Sum of Root To Leaf Binary Numbers

    dfs class Solution: def sumRootToLeaf(self, root: TreeNode) -> int: stack=[(root,0)] ans=[] bi_st ...

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

  8. LeetCode1022. 从根到叶的二进制数之和

    题目 class Solution { public: int ans = 0; int sumRootToLeaf(TreeNode* root) { dfs(root,0); return ans ...

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

随机推荐

  1. 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]

    题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...

  2. 【BZOJ1176】Mokia

    题目大意:给定一个 N*N 的矩形,有 Q 次操作,每个操作可以是矩形单点修改或查询子矩形的权值和. 题解:CDQ分治适合处理修改操作之间互不影响且支持离线的题目. 满足以上操作条件的显然可以树套树来 ...

  3. IC SPEC相关数据

    ---恢复内容开始--- 静态电流:静态电流是指没有信号输入时的电流,也就是器件本身在不受外部因素影响下的本身消耗电流. 纹波电压的害处: 1.容易在用设备中产生不期望的谐波,而谐波会产生较多的危害: ...

  4. Acwing-282-石子合并(区间DP)

    链接: https://www.acwing.com/problem/content/284/ 题意: 设有N堆石子排成一排,其编号为1,2,3,-,N. 每堆石子有一定的质量,可以用一个整数来描述, ...

  5. nodejs之express入门

    首先安装nodejs,官网下载安装包直接安装, 由于node自带npm,所以npm命令直接即可使用 打开cmd,使用npm install -g express-generator安装express ...

  6. 51 Nod N的阶乘的长度 (斯特林近似)

    1058 N的阶乘的长度  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3. Inp ...

  7. 误删系统服务Task Schedule的恢复方法

    cmd命令 sc query Schedule查询该服务是否存在 sc delete Schedule删除服务 sc create Schedule binpath= "C:\Windows ...

  8. Hibernate动态条件查询(Criteria Query)

    1.创建一个Criteria实例net.sf.hibernate.Criteria这个接口代表对一个特定的持久化类的查询.Session是用来制造Criteria实例的工厂. Criteria cri ...

  9. git多人参与的项目 -> 分支代码如何合并到主干

    个人理解:合并分支时候就是当前分支,与别的分支先合并一遍,然后解决分支中存在的所有冲突,之后将本地分支代码提交到git远程仓库,之后切换主干分支 ,将主干分支与分支内容合并,解决冲突, 在提交主干分支 ...

  10. Hibernate与MyBaits的区别?

    (1)Hibernate是全自动,而myBatis是半自动,Hibernate完全可以通过对象关系模型实现对数据库的操作,拥有完整的JavaBean对象与数据库的映射结构来自动生成SQL.而myBat ...