376. Binary Tree Path Sum【LintCode java】
Description
Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target.
A valid path is from root node to any of the leaf nodes.
Example
Given a binary tree, and target = 5:
1
/ \
2 4
/ \
2 3
return
[
[1, 2, 2],
[1, 4]
]
解题:给一个二叉树,找到和是给定目标值的支路。如果不用栈,需要考虑一下回溯算法的运用,代码如下:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/*
* @param root: the root of binary tree
* @param target: An integer
* @return: all valid paths
*/
int target = 0;
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
// write your code here
if(root == null){
return res;
}
this.target = target;
List<Integer>path = new ArrayList<>();
helper(path, root, 0);
return res;
}
public void helper(List<Integer>path, TreeNode root, int sum){
sum +=root.val;
path.add(root.val);
//如果到叶子结点了,并且和为target
if(root.left == null && root.right == null && sum == target){
ArrayList<Integer>temp = new ArrayList<>();
temp.addAll(path);//复制一份
res.add(temp);
}
if(root.left != null){
helper(path, root.left, sum);
}
if(root.right != null){
helper(path, root.right, sum);
}
path.remove(path.size() - 1);
}
}
376. Binary Tree Path Sum【LintCode java】的更多相关文章
- [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II
Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...
- 375. Clone Binary Tree【LintCode java】
Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...
- Binary Tree Path Sum
Given a binary tree, find all paths that sum of the nodes in the path equals to a given number targe ...
- leetcode:Path Sum【Python版】
1.类中递归调用函数需要加self # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # s ...
- 245. Subtree【LintCode java】
Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...
- 408. Add Binary【LintCode java】
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...
- 365. Count 1 in Binary【LintCode java】
Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
随机推荐
- java两种反射的区别 - Class.forName()和ClassLoader.loadClass()
在理解这两种反射机制之前,需要弄清楚java类的加载机制. 装载:通过类的全限定名获取二进制字节流(二进制的class文件),将二进制字节流转换成方法区中的运行时数据结构,在内存中生成Java.lan ...
- 手机站全局的html+css加载等待效果
本文只提供思路,CSS神马的自己定制吧,JS是可以优化的,比如,输出图片的JS也可以放到showdiv()里面,我没有做优化,只是实现,别笑话我,我比较懒... 基本思路:由于Html的解析是从上到下 ...
- 在windows service中启动类型“Automatic” 和 “Automatic (Delayed start)” 有何不同?
问题: When installing Windows services there are two options for automatically starting a Windows serv ...
- 协议类接口 - LCD
一.引脚含义 下图为某LCD相关引脚: 从引脚可以大概看出其SoC的连接情况: 1)VCLK为时钟,每一次像素就移动一次 2)HSYNC/VLINE 3)VSYNC/VFRAME 4)VD0 - VD ...
- 数据库与python的连接
db=web.database( dbn="mysql", host="localhost", port=3306, user="root" ...
- 【转载】 旧版本Microsoft Office正在配置解决方法
原文:https://blog.csdn.net/sinat_37215184/article/details/81053931 在运行Microsoft Office 2010等旧版本的Office ...
- CentOS6的/etc/rc.local不执行的问题解决
小编在一个场景下,希望java -jar abc.jar命令在系统开机自启动是自动执行,于是乎在文件/etc/rc.local中写入nohup java -jar abc.jar &,重启后发 ...
- Java : java基础(6) 反射与枚举
类需要经过 加载, 连接, 初始化三个步骤来进行初始化. 加载是把class文件读入内存创建一个class对象, 连接分为三步,第一步是验证是否是正确的结构, 第二步是准备, 为类的静态成员分配内存, ...
- MFC非模态添加进程控件方法一(线程方法)
由于非模态对话框的自己没有消息循环,创建后无法进行消息处理.需要和父窗口共用消息循环.如果单独在子窗口进行控件由于自己没有单独的消息循环,更新是无法进行的. 如果在父窗口更新控件会造成程序假死.如以下 ...
- python在lxml中使用XPath语法进行#数据解析
在lxml中使用XPath语法: 获取所有li标签: from lxml import etree html = etree.parse('hello.html') print type(html) ...