题目:输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。二叉树结点的定义如下:
struct BinaryTreeNode{
       int      m_nValue;
       BinaryTreeNode*      m_pLeft;
       BinaryTreeNode*      m_pRight;
}

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

代码实现:
package com.yyq;
import java.util.Iterator;
import java.util.Stack;
/**
* Created by Administrator on 2015/9/20.
*/
public class PathInTree {
public static void findPath_1(BinaryTreeNode pRoot, int expectedSum){
if (pRoot == null)
return;
Stack<Integer> path = new Stack<Integer>();
int currentSum = 0;
findPath_2(pRoot, expectedSum, path, currentSum);
}
public static void findPath_2(BinaryTreeNode pRoot,int expectedSum,Stack<Integer> path,int currentSum){
currentSum += pRoot.getM_nValue();
path.push(pRoot.getM_nValue());
//如果是叶结点,并且路径上结点的和等于输入的值
//打印出这条路径
boolean isLeaf = pRoot.getM_pLeft() == null && pRoot.getM_pRight() == null;
if (currentSum == expectedSum && isLeaf){
System.out.println("A path is found:");
Iterator<Integer> it = path.iterator();
while(it.hasNext()){
System.out.print(it.next() + "\t");
}
System.out.println();
}
//如果不是叶结点,则遍历它的子节点
if (pRoot.getM_pLeft()!= null){
findPath_2(pRoot.getM_pLeft(), expectedSum, path, currentSum);
}
if (pRoot.getM_pRight()!=null){
findPath_2(pRoot.getM_pRight(), expectedSum, path, currentSum);
}
// 在返回到父结点之前,在路径上删除当前结点,
// 并在currentSum中减去当前结点的值
currentSum -= pRoot.getM_nValue();
path.pop();
}
// ====================测试代码====================
public static void Test(String testName, BinaryTreeNode pRoot, int expectedSum)
{
if(testName != null)
System.out.println(testName+" begins:");
findPath_1(pRoot, expectedSum);
System.out.println();
}
// 10
// / \
// 5 12
// /\
// 4 7
// 有两条路径上的结点和为22
public static void Test1()
{
BinaryTreeNode pNode10 = new BinaryTreeNode(10);
BinaryTreeNode pNode5 = new BinaryTreeNode(5);
BinaryTreeNode pNode12 = new BinaryTreeNode(12);
BinaryTreeNode pNode4 = new BinaryTreeNode(4);
BinaryTreeNode pNode7 = new BinaryTreeNode(7);
pNode10.connectTreeNodes(pNode5, pNode12);
pNode5.connectTreeNodes(pNode4, pNode7);
System.out.println("Two paths should be found in Test1.");
Test("Test1", pNode10, 22);
pNode10 = null;
}
// 10
// / \
// 5 12
// /\
// 4 7
// 没有路径上的结点和为15
public static void Test2()
{
BinaryTreeNode pNode10 = new BinaryTreeNode(10);
BinaryTreeNode pNode5 = new BinaryTreeNode(5);
BinaryTreeNode pNode12 = new BinaryTreeNode(12);
BinaryTreeNode pNode4 = new BinaryTreeNode(4);
BinaryTreeNode pNode7 = new BinaryTreeNode(7);
pNode10.connectTreeNodes(pNode5, pNode12);
pNode5.connectTreeNodes(pNode4, pNode7);
System.out.println("No paths should be found in Test2.");
Test("Test2", pNode10, 15);
pNode10 =null;
}
// 5
// /
// 4
// /
// 3
// /
// 2
// /
// 1
// 有一条路径上面的结点和为15
public static void Test3()
{
BinaryTreeNode pNode5 = new BinaryTreeNode(5);
BinaryTreeNode pNode4 = new BinaryTreeNode(4);
BinaryTreeNode pNode3 = new BinaryTreeNode(3);
BinaryTreeNode pNode2 = new BinaryTreeNode(2);
BinaryTreeNode pNode1 = new BinaryTreeNode(1);
pNode5.connectTreeNodes(pNode4, null);
pNode4.connectTreeNodes(pNode3, null);
pNode3.connectTreeNodes(pNode2, null);
pNode2.connectTreeNodes(pNode1, null);
System.out.println("One path should be found in Test3.");
Test("Test3", pNode5, 15);
pNode5 = null;
}
// 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
// 没有路径上面的结点和为16
public static void Test4()
{
BinaryTreeNode pNode1 = new BinaryTreeNode(1);
BinaryTreeNode pNode2 = new BinaryTreeNode(2);
BinaryTreeNode pNode3 = new BinaryTreeNode(3);
BinaryTreeNode pNode4 = new BinaryTreeNode(4);
BinaryTreeNode pNode5 = new BinaryTreeNode(5);
pNode1.connectTreeNodes(null, pNode2);
pNode2.connectTreeNodes(null, pNode3);
pNode3.connectTreeNodes(null, pNode4);
pNode4.connectTreeNodes(null, pNode5);
System.out.println("No paths should be found in Test4.");
Test("Test4", pNode1, 16);
pNode1 = null;
}
// 树中只有1个结点
public static void Test5()
{
BinaryTreeNode pNode1 = new BinaryTreeNode(1);
System.out.println("One path should be found in Test5.");
Test("Test5", pNode1, 1);
pNode1 = null;
}
// 树中没有结点
public static void Test6()
{
System.out.println("No paths should be found in Test6.");
Test("Test6", null, 0);
}
public static void main(String[] args)
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
}
}
 
结果输出:
Two paths should be found in Test1.
Test1 begins:
A path is found:
10 5 7
A path is found:
10 12
 
No paths should be found in Test2.
Test2 begins:
 
One path should be found in Test3.
Test3 begins:
A path is found:
5 4 3 2 1
 
No paths should be found in Test4.
Test4 begins:
 
One path should be found in Test5.
Test5 begins:
A path is found:
1
 
No paths should be found in Test6.
Test6 begins:

P143、面试题25:二叉树中和为某一值的路径的更多相关文章

  1. 剑指Offer:面试题25——二叉树中和为某一值的路径(java实现)

    问题描述: 输入一棵二叉树和一个整数,打印出二叉树中结点指的和为输入整数的所有路径.从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.二叉树结点的定义如下: public class Tree ...

  2. 《剑指offer》面试题25 二叉树中和为某一值的路径 Java版

    (判断是否有从根到叶子节点的路径,其和为给定值.记录这些路径.) 我的方法:这道题我是按照回溯的思路去做的,我们需要一个数据结构来保存和删除当前递归函数中添加的值.这里要打印一条路径,我们可以选择Li ...

  3. 【剑指Offer】面试题34. 二叉树中和为某一值的路径

    题目 输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径.从树的根节点开始往下一直到叶节点所经过的节点形成一条路径. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / ...

  4. 《剑指offer》面试题34. 二叉树中和为某一值的路径

    问题描述 输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径.从树的根节点开始往下一直到叶节点所经过的节点形成一条路径. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 ...

  5. 剑指offer 25 二叉树中和为某一值的路径

    非递归方法: class Solution { public: vector<vector<int>> FindPath(TreeNode* root,int expectNu ...

  6. 【剑指Offer面试题】 九度OJ1368:二叉树中和为某一值的路径

    题目链接地址: http://ac.jobdu.com/problem.php? pid=1368 题目1368:二叉树中和为某一值的路径 时间限制:1 秒内存限制:32 兆特殊判题:否提交:2252 ...

  7. 剑指Offer面试题:23.二叉树中和为某一值的路径

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

  8. 《剑指offer》第三十四题(二叉树中和为某一值的路径)

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

  9. 剑指offer 二叉树中和为某一个值的路径

    剑指offer 牛客网 二叉树中和为某一个值的路径 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 15:53:58 2 ...

  10. [PHP]算法-二叉树中和为某一值的路径的PHP实现

    二叉树中和为某一值的路径: 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的li ...

随机推荐

  1. jQuery父级以及同级元素查找介绍

    父级以及同级元素的查找在使用过程中还是蛮频繁的,下面为大家介绍下jQuery是如何实现的,感兴趣的朋友可以参考下: jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$ ...

  2. 锋利的jquery-validation

    jquery插件 jquery插件项目托管于gitHub,项目地址https://github.com/jquery/plugins.jquery.com jquery插件的使用 表单验证插件 现在网 ...

  3. MAC机中安装RUBY环境

    在安装CocoaPods之前要先配置好RUBY环境,本文就怎么安装RUBY的环境进行一总结.安装Ruby环境首先需要安装Xcode然后需要安装Homebrew,接下来需要安装RVM最后安装Ruby环境 ...

  4. laravel--上传

    1.视图里面 2.控制器里面 //收集表单提交数据 $input = $request->all(); //查看是否有图片提交上来 if ($request->hasFile('title ...

  5. php中类的声明与使用

    <?php /**php语言是支持面向对象编程的,对于面向对象的编程,学过java和C++的人都知道啊! *如果不清楚的去baidu问一下就可以了. */ //我们来定义一个类,定义类的关键字是 ...

  6. PHP开发环境和软件

    1/很方便的软件XAMMP集成了PHP+MYSQL+MYPHPADMIN等等软件 2/sublime text 程序员神器,都明白的 ps.如果装了vm虚拟机,80端口有时候会被占用,进程关闭就好.

  7. 解决Execwb 导致 ado崩溃的问题。

    http://qc.embarcadero.com/wc/qcmain.aspx?d=61255

  8. 解决java.io.IOException: Cannot run program "cygpath": CreateProcess error=2, 系统找不到指定的文件 的错误

    一.外部环境: 系统环境:Windows 8 磁盘分区:只有C盘 开发环境:IntelliJ IDEA Community Edition 2016.1.3(64) 执行代码:rdd.saveAsTe ...

  9. VB6-AppendToLog 通过API写入日志

    工作中免不了需要为自己的程序添加日志,我也从网上扒拉了一个老外写的模块,修改修改了下,凑合用吧. Option Explicit '********************************** ...

  10. 找不到 com.google.zxing.ResultMetadataType 异常解决

    在 https://github.com/zxing/zxing 下载二维码扫描 将 android 导入,code打成jar包运行时 报 06-14 23:43:08.690: E/AndroidR ...