二叉树的路径和
来源:互联网 发布:网络信息发布管理办法 编辑:程序博客网 时间:2024/06/01 08:04
/** * 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 */ private List<Integer> middleList = new ArrayList<Integer>(); private int count = 0; private int target = 0; public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) { // Write your code here List<List<Integer>> result = new ArrayList<List<Integer>>(); this.target = target; traversalTree(root, result); return result; } private void traversalTree(TreeNode node , List<List<Integer>> result){ if (node == null){ return; } //前序遍历 int value = node.val; middleList.add(value); count += value; if (count == target&&(node.left == null || node.right == null)){ result.add(new ArrayList(middleList)); count -= value; middleList.remove(middleList.lastIndexOf(value)); return; } traversalTree(node.left, result); traversalTree(node.right, result); middleList.remove(middleList.lastIndexOf(value)); count -= value; }}
阅读全文
0 0
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- 二叉树的路径和
- java多线程之wait、notify和notifyAll
- java的格式化
- Oracle 数据库安装过程
- 获取系统位数
- php生成雪花背景验证码程序代码
- 二叉树的路径和
- 刷机命令
- AI时代的来临
- 号码归属地识别-Android电话应用
- Java-一些问题
- 将二叉查找树转换成双链表
- android 静态注册广播注意事项
- php图像处理(缩放、剪裁、缩放、翻转、旋转、透明、锐化)
- Kubernetes之kubectl常用命令使用指南:2:故障排查