Leetcode 1022. Sum of Root To Leaf Binary Numbers (树遍历题)

这篇具有很好参考价值的文章主要介绍了Leetcode 1022. Sum of Root To Leaf Binary Numbers (树遍历题)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  1. Sum of Root To Leaf Binary Numbers
    Easy
    3.3K
    183
    Companies
    You are given the root of a binary tree where each node has a 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.

The test cases are generated so that the answer fits in a 32-bits integer.

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

Input: root = [0]
Output: 0

Constraints:

The number of nodes in the tree is in the range [1, 1000].
Node.val is 0 or 1.

解法1:文章来源地址https://www.toymoban.com/news/detail-769662.html

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int sumRootToLeaf(TreeNode* root) {
        helper(root, 0);
        return sum;
    }
private:
    int sum = 0;
    void helper(TreeNode *root, int path) {
        if (!root) return;
        path = (path << 1) + root->val;
        if (!root->left && !root->right) {
            sum += path;
            return;
        }
        helper(root->left, path);
        helper(root->right, path);
        return;
    }
};

到了这里,关于Leetcode 1022. Sum of Root To Leaf Binary Numbers (树遍历题)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode //C - 124. Binary Tree Maximum Path Sum

    A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. The path sum of a path is the sum of the node’s values in the path. Given the root of a binary tree, return the maximum

    2024年02月09日
    浏览(39)
  • LintCode 650 · Find Leaves of Binary Tree (binary tree 后序遍历非常好的题目!)

    650 · Find Leaves of Binary Tree Algorithms Medium Description Given a binary tree, collect a tree’s nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty. Example Example1 Input: {1,2,3,4,5} Output: [[4, 5, 3], [2], [1]]. Explanation: / 2 3 / 4 5 Example2 Input: {1,2,3,4} Output: [[4, 3], [2], [1]]. Explanation:

    2024年02月20日
    浏览(38)
  • 【root is not a leaf queue】 org.apache.hadoop.yarn.exceptions.YarnException: Failed to submit appli..

    最近在学习 Hadoop 时,搭建集群环境,用 maven 打 jar 时遇到了 一系列坑点(-_-) : 虚拟机集群搭建时,每完成一部都要 快照 ,后面如果出错,可以恢复上一步快照!!! windows 里面装的 jdk 要和集群环境里面的 jdk 版本最好一致 ,不然有可能 jar 包在 hadoop 集群运行不了!!! 如果

    2023年04月16日
    浏览(38)
  • Leetcode 508. Most Frequent Subtree Sum (二叉树后序遍历好题)

    Most Frequent Subtree Sum Solved Medium Topics Companies Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). Example

    2024年02月22日
    浏览(37)
  • LeetCode --- 1863. Sum of All Subset XOR Totals 解题报告

    The  XOR total  of an array is defined as the bitwise  XOR  of  all its elements , or  0  if the array is  empty . For example, the  XOR total  of the array  [2,5,6]  is  2 XOR 5 XOR 6 = 1 . Given an array  nums , return  the  sum  of all  XOR totals  for every  subset  of  nums .  Note:  Subsets with the  same  elements should be c

    2024年02月15日
    浏览(53)
  • LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    In a linked list of size n, where n is even, the i t h i^{th} i t h node (0-indexed) of the linked list is known as the twin of the ( n − 1 − i ) t h (n-1-i)^{th} ( n − 1 − i ) t h node, if 0 = i = (n / 2) - 1. For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4. Th

    2024年01月17日
    浏览(60)
  • 小白水平理解面试经典题目LeetCode 404 Sum of Left Leaves【Tree】

    给定二叉树的root,返回所有左叶的总和。 叶子是没有子节点的节点。左叶是另一个节点的左子节点的叶。 在大学某个自习的下午,小白坐在教室看到这道题。想想自己曾经和白月光做题,现在大过年的,也是只有自己练题了。左边一颗树,右边一棵树。。。 这时候黑长直女

    2024年02月22日
    浏览(46)
  • 模型评估(误差平方和(SSE The sum of squares due to error))

    举例:(下图中数据-0.2, 0.4, -0.8, 1.3, -0.7, 均为真实值和预测值的差) 在k-means中的应用: 公式各部分内容: 上图中: k=2 SSE图最终的结果,对图松散度的衡量. (eg:  SSE(左图)SSE(右图) ) SSE随着聚类迭代,其值会越来越小,直到最后趋于稳定: 如果质心的初始值选择不好,SSE只会达到一个不怎

    2024年02月04日
    浏览(49)
  • LeetCode //C - 114. Flatten Binary Tree to Linked List

    Given the root of a binary tree, flatten the tree into a “linked list”: The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null. The “linked list” should be in the same order as a pre-order traversal of the binary tree.   Example 1: Input: ro

    2024年02月09日
    浏览(36)
  • Leetcode 1011. Capacity To Ship Packages Within D Days (Binary Search经典)

    Capacity To Ship Packages Within D Days Medium 8.6K 179 Companies A conveyor belt has packages that must be shipped from one port to another within days days. The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maxi

    2024年02月09日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包