[剑指offer学习心得]之:二叉树中和为某一值的路径

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

二叉树结点的定义:

public static class BinaryTreeNode {
    int value;
    BinaryTreeNode left;
    BinaryTreeNode right;
}

解题思路:

由于路径是从根结点出发到叶结点, 也就是说路径总是以根结点为起始点,因此我们首先需要遍历根结点。在树的前序、中序、后序三种遍历方式中,只有前序遍历是首先访问根结点的。

当用前序遍历的方式访问到某一结点时, 我们把该结点添加到路径上,并累加该结点的值。如果该结点为叶结点并且路径中结点值的和刚好等于输入的整数, 则当前的路径符合要求,我们把它打印出来。如果当前结点不是叶结点,则继续访问它的子结点。当前结点访问结束后,递归函数将自动回到它的父结点。因此我们在函数退出之前要在路径上删除当前结点并减去当前结点的值,以确保返回父结点时路径刚好是从根结点到父结点的路径。

不难看出保存路径的数据结构实际上是一个枝, 因为路径要与递归调用状态一致, 而递归调用的本质就是一个压栈和出栈的过程。

测试用例

  1. 功能测试(二叉树中有一条、多条符合条件的路径,二叉树中没有符合条件的路径)
  2. 特殊输入测试(指向二叉树根结点的为null)

代码

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

public class FindPath {

    public static class BinaryTreeNode{
        int value;
        BinaryTreeNode left;
        BinaryTreeNode right;
    }

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

    }

    public static void findPath(BinaryTreeNode root,int curSum,int expectSum,List<Integer> result){
        if(root!=null){
            curSum+=root.value;
            result.add(root.value);

            boolean isLeaf=root.left==null&&root.right==null;
            if(curSum==expectSum&&isLeaf){
                System.out.println(result);
            }else if(curSum<expectSum){
                findPath(root.left,curSum,expectSum,result);
                findPath(root.right,curSum,expectSum,result);
            }
            result.remove(result.size()-1);
        }
    }

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

  // 有两条路径上的结点和为22
  System.out.println("findPath(root, 22);");
  findPath(root, 22);
  // 没有路径上的结点和为15
  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();
  root2.value = 5;
  root2.left = new BinaryTreeNode();
  root2.left.value = 4;
  root2.left.left = new BinaryTreeNode();
  root2.left.left.value = 3;
  root2.left.left.left = new BinaryTreeNode();
  root2.left.left.left.value = 2;
  root2.left.left.left.left = new BinaryTreeNode();
  root2.left.left.left.left.value = 1;

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



//1
  //  \
  //   2
  //    \
  //     3
  //      \
  //       4
  //        \
  //         5
  BinaryTreeNode root3 = new BinaryTreeNode();
  root3.value = 1;
  root3.right = new BinaryTreeNode();
  root3.right.value = 2;
  root3.right.right = new BinaryTreeNode();
  root3.right.right.value = 3;
  root3.right.right.right = new BinaryTreeNode();
  root3.right.right.right.value = 4;
  root3.right.right.right.right = new BinaryTreeNode();
  root3.right.right.right.right.value = 5;
  // 有一条路径上面的结点和为15
  System.out.println("findPath(root3, 15);");
  findPath(root3, 15);
  // 没有路径上面的结点和为16
  System.out.println("findPath(root3, 16);");
  findPath(root3, 16);
  // 树中只有1个结点
  BinaryTreeNode root4 = new BinaryTreeNode();
  root4.value = 1;
  // 有一条路径上面的结点和为1
  System.out.println("findPath(root4, 1);");
  findPath(root4, 1);
  // 没有路径上面的结点和为2
  System.out.println("findPath(root4, 2);");
  findPath(root4, 2);
  // 树中没有结点
  System.out.println("findPath(null, 0);");
  findPath(null, 0);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值