`
hcx2013
  • 浏览: 83605 次
社区版块
存档分类
最新评论

list.remove(list.size()-1);

 
阅读更多

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);
	}
}
 
0
2
分享到:
评论

相关推荐

    删除List中的重复值

    删除List中的重复值 方法一:循环元素删除 ... i &lt; list.size() - 1 ; i ++ ) { for ( int j = list.size() - 1 ; j &gt; i; j -- ) { if (list.get(j).equals(list.get(i))) { list.remove(j); } } }

    list的正确remove姿势

    mList.add(1); mList.add(2); mList.add(3); mList.add(4); mList.add(5); /** * 正常删除 */ for (int i = 0; i &lt; mList.size(); i++) { if (mList.get(i).equals(3)) { mList.remove(i); } } ...

    常见数据结构的Java实现

    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+...

    php.ini-development

    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 ...

    BURNINTEST--硬件检测工具

    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 ...

    Sakemail

    :)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 ...

    LeetCode最全代码

    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-&gt;next; delete p1; } list.size = 0; } void removeAll (InfList & list) { ...

    android开发的计算器

    if(num.size()==symbol.size()) num.add(0.0); if (symbol != null) { for (int i = 0; i &lt; symbol.size(); i++) { if (symbol.get(i).equals("×") || symbol.get(i).equals("÷")) { String ...

    ppt压缩*(PPTminimizer)

    - 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 ...

    EurekaLog_7.5.0.0_Enterprise

    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 ...

    opensc-0.12.0.tar.gz

    * 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...

    Rave Player网页视频播放器

    - 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中的类似于java对象List的工具List.js

    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")); ...

    SQLMemTable for Delphi / C++ Builder

    Maximum field name‘s length: 255 characters.Maximum index name‘s length: 255 characters.BLOB fields block size: &gt; 1 byte, default 100 Kb. BLOB compression: ZLib, BZip, PPM Search operators: &lt;,&gt;,=,,...

    GENESIS 2000 DFM PE

    Layer and List Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Feature Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Shape Structure . . ...

    js模拟list和map

    js/utils.js IE6.0 测试通过 &lt;br&gt;List: add(var obj) //添加一个元素 remove(var index) //删除一个元素 get(var index) //获取一个元素 removeAll() //删除所有元素 toArray() //获取元素数组...

    Senfore_DragDrop_v4.1

    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 ...

    VC 使用CStringArray类创建和使用字符串数组.rar

     case 1://删除操作  if(m_pos||m_pos&gt;=m_array.GetSize())//判断删除位置的有效性  m_array.RemoveAt(m_pos);//在数组的指定位置删除字符串  m_list.ResetContent();//清空列表框  ……

Global site tag (gtag.js) - Google Analytics