剑指offer-问题25

package offer;

import java.util.ArrayList;
import java.util.List;

/**
 * offer interview 25
 */
public class Test25 {
    public static class BinaryTreeNode{
        int value;
        BinaryTreeNode left;
        BinaryTreeNode right;

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

    public static void findPath(BinaryTreeNode root,int expectedSum){
        List<Integer> list = new ArrayList<>();
        if (root != null){
            findPath(root,0,expectedSum,list);
        }
    }

    public static void findPath(BinaryTreeNode root, int curSum,int expectedSum,List<Integer> result){
        if (root != null){
            curSum += root.value;
            result.add(root.value);
            if (curSum < expectedSum){
                findPath(root.left,curSum,expectedSum,result);
                findPath(root.right,curSum,expectedSum,result);
            }else if(curSum == expectedSum){
                if (root.left == null && root.right == null){
                    System.out.println(result);
                }
            }

            result.remove(result.size() -1);

        }
    }

    public static void main(String[] args){
        //            10
        //         /      \
        //        5        12
        //       /\
        //      4  7
        BinaryTreeNode root = new BinaryTreeNode(10);
        root.left = new BinaryTreeNode(5);
        root.right = new BinaryTreeNode(12);
        root.left.left = new BinaryTreeNode(4);
        root.left.right = new BinaryTreeNode(7);

        System.out.println("findPath(root,22):");
        findPath(root,22);

        System.out.println("findPath(root,15):");
        findPath(root,15);

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


        //               5
        //              /
        //             4
        //            /
        //           3
        //          /
        //         2
        //        /
        //       1
        BinaryTreeNode root2 = new BinaryTreeNode(5);
        root2.left = new BinaryTreeNode(4);
        root2.left.left = new BinaryTreeNode(3);
        root2.left.left.left = new BinaryTreeNode(2);
        root2.left.left.left.left = new BinaryTreeNode(1);

        System.out.println("findPath(root2, 15);");
        findPath(root2, 15);

        System.out.println("findPath(root2, 16);");
        findPath(root2, 16);

        // 1
        //  \
        //   2
        //    \
        //     3
        //      \
        //       4
        //        \
        //         5
        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);

        System.out.println("findPath(root3, 15);");
        findPath(root3, 15);

        System.out.println("findPath(root3, 16);");
        findPath(root3, 16);

        BinaryTreeNode root4 = new BinaryTreeNode(1);
        System.out.println("findPath(root4, 1);");
        findPath(root4, 1);

        System.out.println("findPath(root4, 2);");
        findPath(root4, 2);

        System.out.println("findPath(null, 0);");
        findPath(null, 0);

    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值