面试题 25: 二叉树中和为某一值的路径

一. 题目

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

代码请到我的代码库中下载 Point2Offer

二. 代码

package week_5;

import java.util.ArrayList;

/**难度系数:***
 * 剑指offer: 二叉树中和为某一个值的路径
 * 方法: 熟悉二叉搜索树的概念,左子树所有结点小于根节点,右子树所有结点都大于根节点,左右子树仍是二叉搜索树,键值都不相等
 * 测试用例: 递归
 * @author dingding
 * Date:2017-7-11 21:40
 * Declaration: All Rights Reserved!
 */
public class No25 {

    public static void main(String[] args) {
        test1();
        test2();
        test3();
    }

    //solution
    private static void findPath(BinaryTreeNode root,int expectedSum){
        if (root == null) {
            return;
        }
        ArrayList<Integer> paths = new ArrayList<>(); //存储路径
        int currentSum = 0;
        findPath(root, expectedSum,paths,currentSum);
    }

    private static void findPath(BinaryTreeNode root,int expectedSum,
            ArrayList<Integer> paths,int currentSum){
        currentSum +=root.value;
        paths.add(root.value);

        boolean isLeaf = (root.left == null && root.right == null);
        if (currentSum == expectedSum && isLeaf) {
            System.out.println("A path is found: ");
            for (int path:paths){
                System.out.print(" "+path);
            }
            System.out.println();
        }

        //如果不是叶子结点
        if (root.left != null) {
            findPath(root.left, expectedSum, paths, currentSum);
        }
        if (root.right != null) {
            findPath(root.right, expectedSum, paths, currentSum);
        }

        //若当前已是叶子结点,移除当前节点
        paths.remove(paths.size()-1);
    }
    /*====================测试用例================*/
    private static void test1() {
        BinaryTreeNode root = new BinaryTreeNode(10);
        root.left = new BinaryTreeNode(5);
        root.left.left = new BinaryTreeNode(4);
        root.left.right = new BinaryTreeNode(7);
        root.right = new BinaryTreeNode(12);

        // 有两条路径上的结点和为22
        System.out.println("findPath(root, 22);");
        findPath(root, 22);
        System.out.println();

        // 没有路径上的结点和为15
        System.out.println("findPath(root, 15);");
        findPath(root, 15);
        System.out.println();

        // 有一条路径上的结点和为19
        System.out.println("findPath(root, 19);");
        findPath(root, 19);
        System.out.println();
    }

    private static void test2() {
        BinaryTreeNode root3 = new BinaryTreeNode(1);
        root3.right = new BinaryTreeNode(2);
        root3.right.right = new BinaryTreeNode(3);
        root3.right.right.right = new BinaryTreeNode(4);
        root3.right.right.right.right = new BinaryTreeNode(5);

        // 有一条路径上面的结点和为15
        System.out.println("findPath(root3, 15);");
        findPath(root3, 15);
        System.out.println();

        // 没有路径上面的结点和为16
        System.out.println("findPath(root3, 16);");
        findPath(root3, 16);
        System.out.println();

    }

    private static void test3() {
         // 树中没有结点
        System.out.println("findPath(null, 0);");
        findPath(null, 0);
    }

}

class BinaryTreeNode{
    int value;
    BinaryTreeNode left;
    BinaryTreeNode right;

    public BinaryTreeNode(int value){
        this.value = value;
        this.left = null;
        this.right = null;
    }
}




有不妥当之处,麻烦告知:D

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值