zoukankan      html  css  js  c++  java
  • leetcode——113. 路径总和 II

    同看懂做不出来。。。

    class Solution:
        def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
            res=[]
            if not root:
                return []
            def helper(root,sum,tmp):
                if not root:
                    return
                if not root.left and not root.right and sum-root.val==0:
                    tmp+=[root.val]
                    res.append(tmp)
                    return 
                helper(root.left,sum-root.val,tmp+[root.val])
                helper(root.right,sum-root.val,tmp+[root.val])
            helper(root,sum,[])
            return res
    执行用时 :72 ms, 在所有 python3 提交中击败了39.94%的用户
    内存消耗 :19.1 MB, 在所有 python3 提交中击败了5.08%的用户
     
    ——2019.11.21
     

    复习:
    public List<List<Integer>> pathSum(TreeNode root, int sum) { //得用到栈吧
            //遍历所有路径
            List<List<Integer>> list = new ArrayList<>();
            if(root == null){
                return list;
            }
            Stack<Integer> path = new Stack<>();
            pathSum(root,sum,list,path);
            return list;
        }
    
        private void pathSum(TreeNode node, int sum, List<List<Integer>> list,Stack<Integer> path) {
            if(node == null && sum != 0){
                return;
            }
            if(node != null) {
                if (node.left == null && node.right == null && sum == node.val) {
                    path.push(node.val);
                    list.add(new ArrayList<>(path));
                    path.pop();
                } else {
                    path.push(node.val);
                    pathSum(node.left, sum - node.val, list, path);
                    pathSum(node.right, sum - node.val, list, path);
                    path.pop();
                }
            }
        }

    ——2020.7.2

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    skydriver 工具 SDExplorer
    微软出手了:Live Office与Google 文档大PK
    4§7 二次曲面的直纹性
    5§4 二次曲线的直径
    5§5 二次曲线的主直径
    AI第四章 计算智能(1)
    矩阵理论 第五讲 对角化与Jordan标准形
    矩阵理论第 四讲 矩阵的对角化
    5§7 二次曲线方程的化简与分类
    矩阵论 ppt
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11907986.html
Copyright © 2011-2022 走看看