zoukankan      html  css  js  c++  java
  • 剑指 Offer 34. 二叉树中和为某一值的路径

    List<List<Integer>> res = new ArrayList<>();
        public List<List<Integer>> pathSum(TreeNode root, int sum) {
            List<Integer> list = new ArrayList<>();
            trackback(root,sum,list);
            return res;
        }
    
        private void trackback(TreeNode root, int sum, List<Integer> list) {
            if(root == null) return;
            if(root.val==sum && root.left==null && root.right == null) {
                list.add(root.val);
                res.add(new ArrayList<>(list));
                list.remove(list.size()-1);
                return;
            }
            list.add(root.val);
            trackback(root.left,sum - root.val,list);
            trackback(root.right,sum-root.val,list);
            list.remove(list.size()-1);
        }

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    leetcode之String to Integer (atoi)
    初次思考
    leetcode之Reverse Words in a String
    Leetcode之Database篇
    在项目中添加类
    创建项目
    配置Eclipse

    递归
    多态
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/13522512.html
Copyright © 2011-2022 走看看