Java二叉树路径和代码示例

 更新时间:2017年12月14日 15:33:57   作者:我要吃掉太阳啊  
这篇文章主要介绍了Java二叉树路径和代码示例,具有一定借鉴价值,需要的朋友可以参考下。

给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。

一个有效的路径,指的是从根节点到叶节点的路径。

样例

给定一个二叉树,和 目标值 = 5:

  1
 / \
 2 4
 / \
 2 3

返回:

[
 [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
  */
	public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
		// Write your code here
		return dfs(root,new ArrayList<Integer>(),0,new ArrayList<List<Integer>>(),target);
	}
	public List<List<Integer>> dfs(TreeNode root,List<Integer> node, int sum, List<List<Integer>> paths,int target)
	 {
		if(root==null)
		  {
			return new ArrayList<List<Integer>>();
		}
		List<List<Integer>> path=new ArrayList<List<Integer>>();
		if(root.left!=null)
		  {
			List<Integer> nodes=new ArrayList<Integer>();
			if(node!=null)
			  {
				nodes.addAll(node);
			}
			nodes.add(root.val);
			List<List<Integer>> temp=dfs(root.left,nodes,sum+root.val,paths,target);
			if(temp!=null)
			   {
				path.addAll(temp);
			}
		}
		if(root.right!=null)
		  {
			List<Integer> nodes=new ArrayList<Integer>();
			if(node!=null)
			  {
				nodes.addAll(node);
			}
			nodes.add(root.val);
			List<List<Integer>> temp=dfs(root.right,nodes,sum+root.val,paths,target);
			if(temp!=null)
			   {
				path.addAll(temp);
			}
		}
		if(root.left==null&&root.right==null)
		  {
			List<Integer> nodes=new ArrayList<Integer>();
			if(node!=null)
			  {
				nodes.addAll(node);
			}
			nodes.add(root.val);
			if(sum+root.val==target)
			   {
				path.add(nodes);
			} else{
				path=new ArrayList<List<Integer>>();
			}
		}
		return path;
	}
}

referance

java编程求二叉树最大路径问题代码分析

java中继承测试代码分析

总结

以上就是本文关于Java二叉树路径和代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

  • 解决scala.collection.mutable.Map写入的问题

    解决scala.collection.mutable.Map写入的问题

    这篇文章主要介绍了解决scala.collection.mutable.Map写入的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • IDEA连接mysql保姆级教学教程

    IDEA连接mysql保姆级教学教程

    学习使用IDEA的时候,需要连接Database,下面这篇文章主要给大家介绍了关于IDEA连接mysql的保姆级教学教程,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • Spring jpa和mybatis整合遇到的问题解析

    Spring jpa和mybatis整合遇到的问题解析

    有朋友说jpa相比mybatis太难用,多表联合的查询写起来也比较费劲,所以便加入了mybatis的支持,在配置jpa时遇到各种问题,需要修改相关配置文件,下面小编给大家分享下修改配置文件的思路,感兴趣的朋友参考下
    2016-10-10
  • java  设计模式之单例模式

    java 设计模式之单例模式

    这篇文章主要介绍了java 设计模式之单例模式的相关资料,需要的朋友可以参考下
    2017-02-02
  • 解决SpringBoot中使用@Async注解失效的问题

    解决SpringBoot中使用@Async注解失效的问题

    这篇文章主要介绍了解决SpringBoot中使用@Async注解失效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • java调用文心一言API的方法实现过程

    java调用文心一言API的方法实现过程

    Java是一种广泛使用的编程语言,用于开发各种应用程序,下面这篇文章主要给大家介绍了关于java调用文心一言API的方法实现,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • Java、JavaScript、Oracle、MySQL中实现的MD5加密算法分享

    Java、JavaScript、Oracle、MySQL中实现的MD5加密算法分享

    这篇文章主要介绍了Java、JavaScript、Oracle、MySQL中实现的MD5加密算法分享,需要的朋友可以参考下
    2014-09-09
  • idea快速搭建springboot项目的操作方法

    idea快速搭建springboot项目的操作方法

    下面小编就为大家分享一篇idea快速搭建springboot项目的操作方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • Caused by: java.lang.NumberFormatException: For input string: “port“(问题解决)

    Caused by: java.lang.NumberFormatException: For input s

    这篇文章主要介绍了Caused by: java.lang.NumberFormatException: For input string: “port“,本文给大家分享完美解决方法,需要的朋友可以参考下
    2023-01-01
  • Ubuntu 安装 JDK8 的两种方法(总结)

    Ubuntu 安装 JDK8 的两种方法(总结)

    下面小编就为大家带来一篇Ubuntu 安装 JDK8 的两种方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06

最新评论