题解 | #二叉树中和为某一值的路径(二)#

二叉树中和为某一值的路径(二)

http://www.nowcoder.com/practice/b736e784e3e34731af99065031301bca

import java.util.ArrayList;
import java.util.LinkedList;

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
        if(root == null) {
            return ans;
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.addLast(root);
        LinkedList<LinkedList<Integer>> res = new LinkedList<>();
        LinkedList<Integer> tmpNums = new LinkedList<>();
        tmpNums.addLast(root.val);
        res.addLast(tmpNums);
        while(!queue.isEmpty()) {
            int sz = queue.size();
            for(int i = 0; i < sz; i++) {
                TreeNode tmp = queue.removeFirst();
                tmpNums = res.removeFirst();
                if(tmp.left == null && tmp.right == null) {
                    int szNums = tmpNums.size();
                    int tmpSum = 0;
                    for(int j = 0; j < szNums; j++) {
                        int tmpNum = tmpNums.removeFirst();
                        tmpSum += tmpNum;
                        tmpNums.addLast(tmpNum);
                    }
                    if(tmpSum == target) {
                        ArrayList<Integer> t = new ArrayList<>();
                        while(!tmpNums.isEmpty()) {
                            t.add(tmpNums.removeFirst());
                        }
                        ans.add(t);
                    }
                    continue;    
                }
                if(tmp.left != null) {
                    LinkedList<Integer> tmpLeNums = new LinkedList<>();
                    int szNums = tmpNums.size();
                    for(int j = 0; j < szNums; j++) {
                        int tmpNum = tmpNums.removeFirst();
                        tmpNums.addLast(tmpNum);
                        tmpLeNums.addLast(tmpNum);
                    }
                    tmpLeNums.addLast(tmp.left.val);
                    res.addLast(tmpLeNums);
                    queue.addLast(tmp.left);
                }
                if(tmp.right != null) {
                    LinkedList<Integer> tmpLeNums = new LinkedList<>();
                    int szNums = tmpNums.size();
                    for(int j = 0; j < szNums; j++) {
                        int tmpNum = tmpNums.removeFirst();
                        tmpNums.addLast(tmpNum);
                        tmpLeNums.addLast(tmpNum);
                    }
                    tmpLeNums.addLast(tmp.right.val);
                    res.addLast(tmpLeNums);
                    queue.addLast(tmp.right);
                }
            }
        }
        return ans;
    }
}

全部评论

相关推荐

这一集&nbsp;硕士输的很惨
找工作ing10:就是这样不是硕士不愿意脱下长衫,是人家觉得屈才了
点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务