- 浏览: 184840 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
给定了一颗二叉树和一个整数sum,要求我们查找树中是否存在一条从根到叶子节点的和为sum的路径。我们从根节点开始搜索,用DFS,当遍历到一个节点时,从根节点到当前节点的和正好为sum,这时我们还要查看当前节点是否为叶子节点,如果为叶子节点就返回true,否则继续搜索。代码如下:
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.
给定了一颗二叉树和一个整数sum,要求我们查找树中是否存在一条从根到叶子节点的和为sum的路径。我们从根节点开始搜索,用DFS,当遍历到一个节点时,从根节点到当前节点的和正好为sum,这时我们还要查看当前节点是否为叶子节点,如果为叶子节点就返回true,否则继续搜索。代码如下:
/** * 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; if(sum == root.val && root.left == null && root.right == null) return true; return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 270Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 389Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 379Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 483Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 668Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 473The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 434Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 584Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 590Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 905Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 934Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 691Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 857Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 789You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 723For a undirected graph with tre ...
相关推荐
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