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

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 number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.

For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.

Return the sum of these numbers.

LeetCode1022. Sum of Root To Leaf Binary Numbers

Example 1:

Input: [1,0,1,0,1,0,1]
Output: 22
Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22

Note:

1. The number of nodes in the tree is between 1 and 1000.
2. node.val is 0 or 1.
3. The answer will not exceed 2^31 - 1.

Java 实现

class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
}
class Solution {
public int sumRootToLeaf(TreeNode root) {
return def(root, 0);
} public int def(TreeNode root, int sum) {
if (root == null) {
return 0;
}
sum = 2 * sum + root.val;
if (root.left == null && root.right == null) {
return sum;
}
int leftSum = root.left == null ? 0 : def(root.left, sum);
int rightSum = root.right == null ? 0 : def(root.right, sum);
return leftSum + rightSum;
}
}

参考资料

LeetCode 1022. 从根到叶的二进制数之和(Sum of Root To Leaf Binary Numbers)的更多相关文章

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

  2. LeetCode.1022-根到叶路径二进制数之和(Sum of Root To Leaf Binary Numbers)

    这是小川的第381次更新,第410篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第243题(顺位题号是1022).给定二叉树,每个节点值为0或1.每个根到叶路径表示以最高 ...

  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. 4、http协议之二

    URL(Unifrom Resource Locator)简述 相对URL 从当前页面(同一个站点内或同一个文章内引用) 绝对URL 从当前页面或其他页面跳转而来(跨站引用) HTTPD版本<0 ...

  2. Java 基础:单例模式 Singleton Pattern

    1.简介 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式涉及到一个单一的类,该类负责创 ...

  3. Docker 常用命令,自用,持续更

    1.进入容器 docker exec -it 容器id /bin/bash docker exec -it db30f533ee1b /bin/bash 2.复制文件到容器 docker cp 文件路 ...

  4. webpack-merge使用说明

    webpack-merge   配置分离 随着我们业务逻辑的增多,图片.字体.css.ES6以及CSS预处理器和后处理器逐渐的加入到我们的项目中来,进而导致配置文件的增多,使得配置文件书写起来比较繁琐 ...

  5. OpenCV reshape The Matrix is not continuous, thus its number of rows can not be changed

    When using OpenCV  reshape and gets this error: OpenCV Error: Image step is wrong (The matrix is not ...

  6. 利用Git上传项目到github以及遇到的问题

    今天学习如何利用git从本地端上传项目,以及遇到问题的解决方法 1.要有自己的github账号,并创建一个仓库, 2.输入仓库的名称,直接Create 注:记住常见成功后的这个地址,后边要用到: 3. ...

  7. Docs-.NET-C#-指南-语言参考-预处理器指令:#pragma warning(C# 参考)

    ylbtech-Docs-.NET-C#-指南-语言参考-预处理器指令:#pragma warning(C# 参考) 1.返回顶部 1. #pragma warning(C# 参考) 2015/07/ ...

  8. Python脚本实现Linux/MAC中Xmind Zen去水印等其他功能的过程(V0.1)

    说明本脚本仅作为学习使用,请勿用于任何商业用途.本文为原创,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明.功能简介 去除软件右上角激活按钮 去除导出时激活弹窗 去除导出PDF文 ...

  9. python3以post方式提交数据

    # !/usr/bin/python # encoding:utf-8 import requests #下面替换成您的数据 postdata={'name':'xiaochong'} r=reque ...

  10. ES6深入浅出-4 迭代器与生成器-5.科班 V.S. 培训

    为什么要学用不到的东西 科班是把你未来一二十年用的东西都给你入个门 做前端 三年后一定要再学一门语言. 买一本图解算法 培训讲究的是技能,只能满足3到5年,而不是术,学术学的是你未来10年甚至20年用 ...