[剑指offer学习心得]之:二叉树中和为某一值的路径
来源:互联网 发布:淘宝库存不足 编辑:程序博客网 时间:2024/06/05 15:59
题目:输入一棵二叉树和一个整数, 打印出二叉树中结点值的和为输入整数的所有路径。从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
二叉树结点的定义:
public static class BinaryTreeNode { int value; BinaryTreeNode left; BinaryTreeNode right;}
解题思路:
由于路径是从根结点出发到叶结点, 也就是说路径总是以根结点为起始点,因此我们首先需要遍历根结点。在树的前序、中序、后序三种遍历方式中,只有前序遍历是首先访问根结点的。
当用前序遍历的方式访问到某一结点时, 我们把该结点添加到路径上,并累加该结点的值。如果该结点为叶结点并且路径中结点值的和刚好等于输入的整数, 则当前的路径符合要求,我们把它打印出来。如果当前结点不是叶结点,则继续访问它的子结点。当前结点访问结束后,递归函数将自动回到它的父结点。因此我们在函数退出之前要在路径上删除当前结点并减去当前结点的值,以确保返回父结点时路径刚好是从根结点到父结点的路径。
不难看出保存路径的数据结构实际上是一个枝, 因为路径要与递归调用状态一致, 而递归调用的本质就是一个压栈和出栈的过程。
测试用例
- 功能测试(二叉树中有一条、多条符合条件的路径,二叉树中没有符合条件的路径)
- 特殊输入测试(指向二叉树根结点的为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); }}
0 0
- [剑指offer学习心得]之:二叉树中和为某一值的路径
- 剑指offer:二叉树中和为某一值的路径
- 【剑指offer】二叉树中和为某一值的路径
- 剑指offer--二叉树中和为某一值的路径
- 《剑指offer》二叉树中和为某一值的路径
- 【剑指Offer】二叉树中和为某一值的路径
- 剑指Offer--二叉树中和为某一值的路径
- 剑指offer:二叉树中和为某一值的路径
- 剑指offer 二叉树中和为某一值的路径
- 剑指offer:二叉树中和为某一值的路径
- [剑指offer]二叉树中和为某一值的路径
- 《剑指offer》-二叉树中和为某一值的路径
- 剑指offer 二叉树中和为某一值的路径
- 【剑指offer】二叉树中和为某一值的路径
- 剑指offer|二叉树中和为某一值的路径
- 《剑指offer》二叉树中和为某一值的路径
- 剑指offer 二叉树中和为某一值的路径
- 剑指Offer:二叉树中和为某一值的路径
- 关于C中指针的引用,解引用与脱去解引用
- 在Ubuntu16.04下搭建samba,实现linux与windows之间的
- Android 中取得当前时区(以GMT形式)和语言
- javascript面向对象编程
- tomcat8安装和配置
- [剑指offer学习心得]之:二叉树中和为某一值的路径
- javascript 判断 flash 插件是否安装
- IntelliJ IDEA :decompiled.class file bytecode version:52.0(java 8) ,open source file.
- git 使用详解(10)-- 远程分支
- Java Web开发Tomcat中三种部署项目的方法
- 关于mysql.xml文件中 #{_parameter}的说明
- jfinal问题整理(转载自开源中国RunnerOSC文章 )
- Sql Server_加外键时值矛盾引起的问题
- 二维码