4.9---二叉树路径和(CC150)

//注意,1,要判断null;2,要注意ArrayList直接复制会被一起改变。要通过new的方式来操作。public class Solution {
    public static void main(String[] args){
        TreeNode root = new TreeNode(10);
        root.left = new TreeNode(5);
        root.right = new TreeNode(12);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(7);
        ArrayList<ArrayList<Integer>> test = FindPath(null,22);
        for(ArrayList<Integer> tmp : test){
            System.out.println(tmp);
        }
    }
    public static ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        ArrayList<ArrayList<TreeNode>> res = new ArrayList();

        ArrayList<ArrayList<Integer>> ans = new ArrayList();
        if(root == null || root.val > target  ) return ans;
        ArrayList<TreeNode> tmp = new ArrayList();
        tmp.add(root);
        res.add(tmp);
        int flag = 1;
        while(flag != 0){
            System.out.println(123);
            flag = 0;
            ArrayList<ArrayList<TreeNode>> res2 = new ArrayList();
            for(ArrayList<TreeNode> a : res){
                ArrayList<TreeNode> left = new ArrayList(a);
                ArrayList<TreeNode> right = new ArrayList(a);

                TreeNode tmp1 = a.get(a.size()-1).left;
                TreeNode tmp2 = a.get(a.size()-1).right;
                if(tmp1 != null ){
                    System.out.println("tmp1=" + tmp1.val);
                    left.add(tmp1);
                    res2.add(left);

                    flag = 1;
                }

                if(tmp2 != null ){
                    right.add(tmp2);
                    res2.add(right);
                    flag = 1;
                }
                if(tmp1 == null && tmp2 == null){
                    res2.add(left);
                }

            }
            res = new ArrayList(res2);
            for(ArrayList<TreeNode> a : res){
                for(TreeNode t : a){
                    System.out.print(t.val+" ");
                }
                System.out.println("");
            }
            System.out.println(flag);
            System.out.println("res.size()="+ res.size());
        }

        for(ArrayList<TreeNode> a : res){
            if(sum(a) == target){
                ArrayList<Integer> al = new ArrayList();
                for(TreeNode t : a){
                    al.add(t.val);
                }

                ans.add(al);
            }
        }

        return ans;
    }

    public static int sum (ArrayList<TreeNode> tmp){
        int sum = 0;
        for(TreeNode t : tmp){
            sum += t.val;
        }
        return sum;
    }
}
时间: 2024-08-04 19:22:11

4.9---二叉树路径和(CC150)的相关文章

Leetcode:Path Sum 二叉树路径和

Path Sum: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return

笔试算法题(06):最大连续子数组和 &amp; 二叉树路径和值

出题:预先输入一个整型数组,数组中有正数也有负数:数组中连续一个或者多个整数组成一个子数组,每个子数组有一个和:求所有子数组中和的最大值,要求时间复杂度O(n): 分析: 时间复杂度为线性表明只允许一遍扫描,当然如果最终的最大值为0表明所有元素都是负数,可以用线性时间O(N)查找最大的元素.具体算法策略请见代码和注释: 子数组的起始元素肯定是非负数,如果添加的元素为正数则记录最大和值并且继续添加:如果添加的元素为负数,则判断新的和是否大于0,如果小于0则以下一个元素作为起始元素重新开始,如果大于

Path Sum II 二叉树路径之和之二

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] ] 这道二叉树路径之和在之前的基础上又需要找出路径 (可

LeetCode (12) Path Sum (二叉树路径和判断)

题目描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, return true, as there exist a root-to-lea

LeetCode 257. Binary Tree Paths (二叉树路径)

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 题目标签:Tree 这道题目给了我们一个二叉树,让我们记录所有的路径,返回一个array string list. 我们可以另外设一个fin

PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

<pre class="code"><span style="font-family: %value; font-size: 14px;">03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each inpu

leetCode 257. Binary Tree Paths 二叉树路径

257. Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree:    1  /   2     3    5 All root-to-leaf paths are: ["1->2->5", "1->3"] 思路: 1.采用二叉树的后序遍历非递归版 2.在叶子节点的时候处理字

C语言强化(四) 求和为某个值的二叉树路径

递归究竟有多强大,看看这道题就知道了. 通过这道题,你可以掌握 如何使用递归 递归的本质 如何跳出递归死循环 题目:输入一个整数和一棵二元树. 从树的[根结点]开始往下访问一直到[叶结点]所经过的所有结点形成一条路径. 打印出和与输入整数相等的所有路径. 例如,输入20和如下二叉树 打印出路径  10 6 4 思路 当访问到某一结点时,把该结点添加到路径上,并累加当前结点的值. 如果当前结点为叶结点并且当前路径的和刚好等于输入的整数,则当前的路径符合要求, 我们把它打印出来. 如果当前结点不是叶

二叉树路径和为某一整数

题目描述:输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. #include<iostream> #include <vector> struct BinaryTreeNode{ int m_nValue; BinaryTreeNode * m_pRight; BinaryTreeNode * m_pLeft; }; void FindPath4Vars( BinaryTreeNode *

UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

Tree UVA - 548 题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的.还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的. 最后输出来叶子节点. 一开始写的时候是用gets读入的,报CE, 要用fgets写,关于fgets(),传送门: fgets函数及其用法,C语言fgets函数详解 一开始用bfs过的,后来发现,好多人都是dfs过的,又写了一下dfs... 代码: 1 //二叉树的中序和后序遍历还原树并输出最短路径的叶子