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

题目:输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径。

public void findPath(Node node,int k){
        if(node == null) return;
        Stack<Integer> stack = new Stack<Integer>();
        findPath(node,k,stack);
    }
    public void findPath(Node root,int k,Stack<Integer> path){
        if(root == null) return ;
        if(root.left == null && root.right ==null){
            if(root.value == k){
                for(int i:path){
                    System.out.print(i+" ");
                }
                System.out.print(root.value);
            }
        }else{
            stack.push(root.value);
            finPath(root.left,k-root.value,path);
            findPath(root.right,k-root.value,path);
        }
    }
原文地址:https://www.cnblogs.com/yingpu/p/9282044.html