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

    解题:前序遍历加上筛选

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        LinkedList<Integer> list = new LinkedList<>();
        LinkedList<List<Integer>> res = new LinkedList<>();
        public List<List<Integer>> pathSum(TreeNode root, int sum) {
        helper(root,sum,list);
        return res;
        }
        //先序遍历
        public void helper(TreeNode root,int sum,LinkedList<Integer> list){
            if(root == null) return ;
             list.add(root.val);
             sum -= root.val;
            if(sum == 0 && root.left == null && root.right ==null){
                res.add(new LinkedList(list));
              //  return ;
            }
            helper(root.left,sum,list);
            helper(root.right,sum,list); 
            list.removeLast();
        }
    }
    
    
    不一样的烟火
  • 相关阅读:
    指针
    显示和隐式转换
    C++虚函数
    字符串输出
    BP神经网络
    超像素分割
    函数putText()在图片上写文字
    compare
    十五、cookies和session的使用
    爬取腾讯社招职位信息
  • 原文地址:https://www.cnblogs.com/cstdio1/p/13368525.html
Copyright © 2011-2022 走看看