zoukankan      html  css  js  c++  java
  • 113. Path Sum II

    找从头到尾SUM=TARGET的路径

    就RECURSION就行

    注意得SUM==TARGET 还得正好是ROOT-LEAF
    然后没啥难的了 好像可以用数据结构做 比如LINKEDLIST不过貌似还是得迭代。。

    public List<List<Integer>> pathSum(TreeNode root, int sum) {
            List<List<Integer>> res = new ArrayList<List<Integer>> ();
    
            if(root == null) return res;
            
    
            helper(root,sum,res, new ArrayList<Integer>());
    
            
          	return res;
    
        }
    
        public void helper(TreeNode root, int sum, List<List<Integer>> res, List<Integer> tempList)
        {
            //System.out.println(root.val + " " + sum);
        	if(root.left == null && root.right == null && root.val == sum)
        	{
        		tempList.add(root.val);
        		res.add(new ArrayList<Integer>(tempList));
        		return;
        	}
        	else
        	{
        		tempList.add(root.val);
    
        		if(root.left != null) helper(root.left,sum-root.val,res,new ArrayList<Integer>(tempList));
        		if(root.right != null) helper(root.right,sum-root.val,res, new ArrayList<Integer>(tempList));
        		return;
        	}
     
        }
    




    二刷。

    先Recursion做了一下。

    public class Solution 
    {
    
        public List<List<Integer>> pathSum(TreeNode root, int sum) 
        {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            if(root == null) return res;
            List<Integer> tempList = new ArrayList<>();
            tempList.add(root.val);
            
            helper(res,root,sum-root.val,tempList);
            
            return res;
        }
        
        public void helper(List<List<Integer>> res, TreeNode root, int sum, List<Integer> tempList)
        {
            if(sum == 0 && root.left == null && root.right == null) res.add(new ArrayList<>(tempList));
            else
            {
                if(root.left != null)
                {
                    tempList.add(root.left.val);
                    helper(res,root.left,sum-root.left.val,new ArrayList<>(tempList));
                    tempList.remove(tempList.size()-1);
                }
                if(root.right != null)
                {
                    tempList.add(root.right.val);
                    helper(res,root.right,sum-root.right.val,new ArrayList<>(tempList));
                }
            }
            
            
        }
    }
    

    再试试Iterative.

    失败。。
    

    Discussion里大同小异。都是recursiion.



    三刷。

    DFS就行了,白送的题。

    Recursion:

    public class Solution {
        public List<List<Integer>> pathSum(TreeNode root, int sum) {
            List<List<Integer>> res = new ArrayList<>();
            if (root == null) return res;
            dfs(res, root, sum, new ArrayList<>());
            return res;
        }
        
        public void dfs(List<List<Integer>> res, TreeNode root, int sum, List<Integer> tempList) {
            if (root == null) return;
            tempList.add(root.val);
            if (root.left == null && root.right == null) {
                if (sum - root.val == 0) {
                    res.add(new ArrayList<>(tempList));
                }
            } else {
                dfs(res, root.left, sum - root.val, tempList);
                dfs(res, root.right, sum - root.val, tempList);
            }
            tempList.remove(tempList.size() - 1);
        }
    }
    

    Iteration:

    尝试了一下不好做,tempList里的元素添加顺序要求是preOrder的,但是遍历顺序只能满足一跳路径是如此。

    只能找到最左那条路,如果用iteration.

  • 相关阅读:
    zt:HttpUrlConnection使用详解
    使用HttpUrlConnection访问www.163.com遇到503问题,用设置代理加以解决
    『TensorFlow』第十弹_队列&多线程_道路多坎坷
    『TensorFlow』项目资源分享
    『Python』socket网络编程
    『TensorFlow』第九弹_图像预处理_不爱红妆爱武装
    『TensorFlow』迁移学习
    『TensorFlow』函数查询列表_神经网络相关
    『TensorFlow』函数查询列表_张量属性调整
    『TensorFlow』函数查询列表_数值计算
  • 原文地址:https://www.cnblogs.com/reboot329/p/6116768.html
Copyright © 2011-2022 走看看