首页 > 其他 > 详细

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

时间:2018-07-09 10:40:10      阅读:139      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!