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<List<Integer>> res = new LinkedList<>();
        LinkedList<Integer> path = new LinkedList<>(); 
        public List<List<Integer>> pathSum(TreeNode root, int sum) {
            recur(root, sum);
            return res;
        }
        void recur(TreeNode root, int tar) {
            if(root == null) return;
            path.add(root.val);
            tar -= root.val;
            if(tar == 0 && root.left == null && root.right == null)
                res.add(new LinkedList(path));//深拷贝
            recur(root.left, tar);
            recur(root.right, tar);
            path.removeLast();
        }
    }
    
  • 相关阅读:
    bom案例2-弹出层
    bom案例1-div拖拽
    bom-scroll
    bom-client
    bom-offset
    9. 阻塞队列
    8. 读写锁
    7. CountDownLatch、CyclicBarrier、Semaphore
    6. Callable
    5. 集合不安全
  • 原文地址:https://www.cnblogs.com/treasury/p/12823250.html
Copyright © 2011-2022 走看看