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] ]
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> pathSum(TreeNode root, int sum) { List<List<Integer>> res = new ArrayList<List<Integer>>(); List<Integer> list = new ArrayList<Integer>(); solve(root, sum, res, list); return res; } private void solve(TreeNode root, int sum, List<List<Integer>> res, List<Integer> list) { if (root == null) { return; } list.add(root.val); sum -= root.val; if (root.left == null && root.right == null) { if (sum == 0) { res.add(new ArrayList<Integer>(list)); } } else { if (root.left != null) { solve(root.left, sum, res, list); } if (root.right != null) { solve(root.right, sum, res, list); } } list.remove(list.size()-1); } }
相关推荐
删除List中的重复值 方法一:循环元素删除 ... i < list.size() - 1 ; i ++ ) { for ( int j = list.size() - 1 ; j > i; j -- ) { if (list.get(j).equals(list.get(i))) { list.remove(j); } } }
mList.add(1); mList.add(2); mList.add(3); mList.add(4); mList.add(5); /** * 正常删除 */ for (int i = 0; i < mList.size(); i++) { if (mList.get(i).equals(3)) { mList.remove(i); } } ...
list.remove(1); list.set(0,"open"); number=list.size(); System.out.println("现在链表中有"+number+"个节点:"); for(int i=0;i;i++) { String temp=(String)list.get(i); System.out.println("第"+i+...
1. SAPI module specific location. ; 2. The PHPRC environment variable. (As of PHP 5.2.0) ; 3. A number of predefined registry keys on Windows (As of PHP 5.2.0) ; 4. Current working directory (except ...
1) Uninstall any previous version of BurnInTest 2) Double click (or Open) the downloaded ".exe" file 3) Follow the prompts UnInstallation ============== Use the Windows control panel, Add / Remove ...
:)26/3/981.6.1- Added a FUNCFileName private variable to manage the complete path of the attached file. I receive problems reports with this, it work now ?.- Reduced the line sleep to 30 (tell me if ...
27 | [Remove Element](https://leetcode.com/problems/remove-element/) | [C++](./C++/remove-element.cpp) [Python](./Python/remove-element.py) | _O(n)_ | _O(1)_ | Easy || 31 | [Next Permutation]...
void removeAll1 (CourseList & list) { Student1 *p1; while(list.head != NULL) { p1 = list.head; list.head = list.head->next; delete p1; } list.size = 0; } void removeAll (InfList & list) { ...
if(num.size()==symbol.size()) num.add(0.0); if (symbol != null) { for (int i = 0; i < symbol.size(); i++) { if (symbol.get(i).equals("×") || symbol.get(i).equals("÷")) { String ...
- ADDED : Possibility to add and remove presentations to/from the list. - ADDED : Adjustment of the Outlook Plug-In while sending an E-Mail in Outlook. - ADDED : Adjustment of the Outlook Plug-In ...
1)..Important: Installation layout was changed. All packages now have version suffix (e.g. EurekaLogCore240.bpl). No files are copied to \bin folder of IDE. Run-time package (EurekaLogCore) is copied ...
* pkcs11-tool can use a slot based on ID, label or index in the slot list. * PIN flags are updated from supported cards when C_GetTokenInfo is called. * Support for CardOS 4.4 cards added. * Fature to...
- Corrected scrubber issue for MP3 files without file size data (or servers that don't issue file size headers). v2.0.29 Apr. 30, 2012 - Changed fullscreen to true fullscreen. v2.0.28 Apr. 24, 2012 -...
js中终于有了自己的List,类似于java中的List对象,本文件是源码,亲测有如下方法:add()、value、size()、get(index)、contains(key)、remove(index)、removeAll()。。。
// Remove elements from the array list al.remove("F"); al.remove(2); System.out.println("Contents of al: " + al); // System.out.println(al.get(2)); // System.out.println(al.indexOf("E")); ...
Maximum field name‘s length: 255 characters.Maximum index name‘s length: 255 characters.BLOB fields block size: > 1 byte, default 100 Kb. BLOB compression: ZLib, BZip, PPM Search operators: <,>,=,,...
Layer and List Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Feature Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Shape Structure . . ...
js/utils.js IE6.0 测试通过 <br>List: add(var obj) //添加一个元素 remove(var index) //删除一个元素 get(var index) //获取一个元素 removeAll() //删除所有元素 toArray() //获取元素数组...
1. Supported platforms 2. Installation 3. Getting started 4. Known problems 5. Support and feedback 6. Bug reports 7. Upgrades and bug fixes 8. Missing in this release 9. New in version 4.x 10. TODO ...
case 1://删除操作 if(m_pos||m_pos>=m_array.GetSize())//判断删除位置的有效性 m_array.RemoveAt(m_pos);//在数组的指定位置删除字符串 m_list.ResetContent();//清空列表框 ……