[Leetcode] Path Sum I,II,III
来源:互联网 发布:以色列 科技 知乎 编辑:程序博客网 时间:2024/05/17 04:36
112. Path Sum I: 点击打开链接
/** * Definition for a binary tree node. //判断是否有这样的路径满足和为sum * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean hasPathSum(TreeNode root, int sum) { return helper(root,sum); } public boolean helper(TreeNode root,int sum){ if(root==null){ return false; } if(root.left==null && root.right==null){ if(root.val==sum){ return true; } } return helper(root.left,sum-root.val) || helper(root.right,sum-root.val); }}
113. Path Sum II:点击打开链接
/** * Definition for a binary tree node. //写出从根节点开始的和为sum的每一条路径 * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<List<Integer>> pathSum(TreeNode root, int sum) { List<List<Integer>> result=new ArrayList<>(); List<Integer> path=new ArrayList<>(); helper(root,sum,result,path); return result; } public void helper(TreeNode root,int target,List<List<Integer>> result,List<Integer> path){ if(root==null){ return; } if(root.left==null && root.right==null){ if(root.val==target){ path.add(root.val); result.add(path); return; } } if(root.left!=null){ List<Integer> left=new ArrayList<>(path); //下一层的添加在之前的基础上添加,因此deep copy left.add(root.val); helper(root.left,target-root.val,result,left); } if(root.right!=null){ List<Integer> right=new ArrayList<>(path); right.add(root.val); helper(root.right,target-root.val,result,right); } }}437. Path Sum III:点击打开链接
/** * Definition for a binary tree node. //数出从任意节点开始的和为sum的路径的条数 * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int pathSum(TreeNode root, int sum) { //根节点开始的满足条件的路径和 + 左子树的情况 + 右子树的情况 if (root == null) return 0; return helper(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum); } private int helper(TreeNode node, int sum) { //从任意node开始满足条件的路径和 if (node == null) return 0; int left=helper(node.left, sum - node.val); int right=helper(node.right, sum - node.val); return (node.val == sum ? 1 : 0) +left+right; }}
0 0
- [Leetcode] Path Sum I,II,III
- Leetcode--Path Sum I,II,III
- Path Sum I II III
- 【LeetCode】112,113,437. Path Sum I, II, III
- LeetCode:Path Sum I &&II
- 【LeetCode】path-sum i&ii
- leetcode Combination Sum I/II/III/IV
- LeetCode题解:Path Sum I and II
- LeetCode Solutions : Path Sum I & II
- LeetCode题解:Path Sum I and II
- LeetCode 112, 123. Path Sum i, ii
- LeetCode 112&113 --Path Sum I & II;
- LeetCode-Path Sum III
- path sum iii-leetcode
- Path Sum III ---LeetCode
- Leetcode | Path Sum III
- Leetcode Path Sum III
- LEETCODE--Path Sum III
- nginx的指令开发时变量处理二
- 226. Invert Binary Tree -- 二叉树、递归、交换、栈
- pageController 页面跳转
- Maximum Subarray 解题报告
- 商品管理service和controller
- [Leetcode] Path Sum I,II,III
- 线性方程组 精确解 近似解 算法整理
- 虚拟存储器
- Lab 3
- 【HTML】碎片知识点-02
- 专业人士对指针的精解
- 数列的逆序数对(难度系数:2颗星)
- 我所理解的MVC架构
- Python学习之While循环