[Leetcode][JAVA] Path Sum I && II

來源:互聯網
上載者:User

標籤:

Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5             /             4   8           /   /           11  13  4         /  \              7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

 

分治法可以解決,傳入下層的sum為原來的sum-root.val即可

1 public boolean hasPathSum(TreeNode root, int sum) {2         if(root==null)3             return false;4         if(root.left==null && root.right==null)5             return root.val==sum;6         return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val);7 }

 

Path Sum II

 

Given a binary tree and a sum, find all root-to-leaf paths where each path‘s sum equals the given sum.

For example:
Given the below binary tree and sum = 22,

              5             /             4   8           /   /           11  13  4         /  \    /         7    2  5   1

 

return

 

[   [5,4,11,2],   [5,8,4,5]]

 

II就是要返回所有可能的path. 可以用分治法的思想去實現(把根節點加到左子樹得到的list和右子樹得到的list的第一位),不過較慢,因為要結果返回給上層。用單純的dfs回溯也能很好地實現,而且較快。

分治法:

 1  public List<List<Integer>> pathSum(TreeNode root, int sum) { 2         List<List<Integer>> re = new ArrayList<List<Integer>>(); 3         if(root==null) 4             return re; 5         if(root.left==null && root.right==null && root.val==sum) { 6             List<Integer> temp = new ArrayList<Integer>(); 7             temp.add(root.val); 8             re.add(temp); 9             return re;10         }11         List<List<Integer>> left = pathSum(root.left, sum-root.val);12         List<List<Integer>> right = pathSum(root.right, sum-root.val);13         if(left.size()>0)14             for(int i=0;i<left.size();i++) {15                 left.get(i).add(0,root.val);16                 re.add(left.get(i));17             }18         if(right.size()>0)19             for(int i=0;i<right.size();i++) {20                 right.get(i).add(0,root.val);21                 re.add(right.get(i));22             }23         return re;24     }

dfs:

public List<List<Integer>> pathSum(TreeNode root, int sum) {        List<List<Integer>> re = new ArrayList<List<Integer>>();        if(root==null)            return re;        List<Integer> path = new ArrayList<Integer>();        collect(re, path, root, sum);        return re;    }        public void collect(List<List<Integer>> re, List<Integer> path, TreeNode rt, int v) {        if(rt.left==null && rt.right==null && rt.val==v) {            List<Integer> temp = new ArrayList<Integer>(path);            temp.add(rt.val);            re.add(temp);            return;        }        path.add(rt.val);        if(rt.left!=null)            collect(re,path,rt.left,v-rt.val);        if(rt.right!=null)            collect(re, path, rt.right, v-rt.val);        path.remove(path.size()-1);    }

 

[Leetcode][JAVA] Path Sum I && II

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.