Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of path summation of leetcode trees

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to solve the leetcode tree path summation problem, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Given a binary tree and a goal sum, determine whether there is a path from the root node to the leaf node in the tree, and the value of all nodes on this path is equal to the goal sum. Note: a leaf node is a node that does not have child nodes. Example: given the following binary tree, and the target and sum = 22,5 /\ 48 / /\ 11 13 4 /\\ 7 21 returns true, because there is a path from the root node to the leaf node 5-> 4-> 11-> 2 for the target and 22. Source: LeetCode link: https://leetcode-cn.com/problems/path-sum copyright belongs to the collar buckle network. For commercial reprint, please contact official authorization. For non-commercial reprint, please indicate the source. Answer the question / Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode (int x) {val = x;} *} * / class Solution {public boolean hasPathSum (TreeNode root, int sum) {if (root = = null) {return false;} if (root.left = = null & & root.right = = null) {return sum-root.val = = 0 } return hasPathSum (root.left, sum-root.val) | | hasPathSum (root.right, sum-root.val);}} Thank you for reading this article carefully. I hope the article "how to solve the path Sum problem of leetcode trees" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

  • Building hadoop Cluster (2) High availability of YARN

    [root@hadoop01 ~] # vim / usr/local/hadoop-2.7.1/etc/hadoop/yarn-site.xml

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report