Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22
,
5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) { return false; } LinkedList<TreeNode> linkedList = new LinkedList<TreeNode>(); LinkedList<Integer> linkedList2 = new LinkedList<Integer>(); linkedList.add(root); linkedList2.add(root.val); while (!linkedList.isEmpty()) { TreeNode poll = linkedList.poll(); Integer poll2 = linkedList2.poll(); if (poll.left == null && poll.right == null && sum == poll2) { return true; } if (poll.left != null) { linkedList.add(poll.left); linkedList2.add(poll.left.val+poll2); } if (poll.right != null) { linkedList.add(poll.right); linkedList2.add(poll.right.val+poll2); } } return false; } }
相关推荐
java java_leetcode题解之Path Sum III.java
public int PathSum(TreeNode root, int sum) { _count = 0; _sum = sum; Travel(root, new List()); return _count; } private void Travel(TreeNode current, List<int> ret) { if (current == null) { ...
LeetCode — Path Sum III分析及实现方法 题目描述: You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need...
113. Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree...
在此过程中,可以使用`__pathSum`函数进行DFS来查找以当前节点为起点的满足条件的路径。 2. **DFS(深度优先搜索)**:`__pathSum`函数通过递归遍历从当前节点开始的所有可能分支。每次进入新的节点,都会更新当前...
类中有一个公开方法`pathSum`,这是主要的接口函数,接受一个树的根节点`root`和目标和`sum`作为参数。 `pathSum`函数首先检查根节点是否为空,如果为空则直接返回空路径列表。然后调用`recursion`辅助函数进行深度...
在上述代码中,`maxPathSum`函数是核心,它接收一个表示数字三角形的二维向量,返回最大路径和。`main`函数则负责读取输入数据,初始化三角形,并调用`maxPathSum`计算结果。 值得注意的是,实际问题可能涉及到更...
public List<List<Integer>> pathSum(TreeNode root, int targetSum) { dfs(root, targetSum); return ret; } public void dfs(TreeNode root, int targetSum) { // ...(其他代码) } } ``` 在DFS中,时间...
java java_leetcode-112-path-sum
`maxPathSum`函数计算最大路径和,它使用了一个全局变量`global_max`来记录全局的最大路径和,以及一个内部的`dfs`函数进行深度优先搜索。`dfs`函数返回以当前节点为根的最大路径和,同时更新`max_path`。 在求职...
python python_leetcode题解之112_Path_Sum
java java_leetcode-113-path-sumII
vector<vector<int>> pathSum(TreeNode* root, int sum) { vector<vector<int>> result; vector<int> path; dfs(root, sum, path, result); return result; } void dfs(TreeNode* node, int target, vector...
javascript js_leetcode题解之112-path-sum.js
python python_leetcode题解之113_Path_Sum_II
int pathSum = std::accumulate(path.begin(), path.end(), 0); // 如果到达叶子结点且路径和等于期望值 if (!root->m_pLeft && !root->m_pRight && pathSum == expectNumber) result.push_back(path); Find...
在主函数中,提供数字三角形的输入数据,并调用maxPathSum函数求解最大路径和。 数字三角形问题不仅锻炼了程序员的逻辑思维能力,也是对动态规划思想的一种实践应用。通过理解和掌握这种算法,可以进一步拓展到其他...
javascript js_leetcode题解之113-path-sum-ii.js