剑指Offer面试题:23.二叉树中和为某一值的路径
一、题目:二叉树中和为某一值的路径
题目:输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。例如输入下图中二叉树和整数22,则打印出两条路径,第一条路径包含结点10、12,第二条路径包含结点10、5和7。
二叉树结点的定义如下:
public class BinaryTreeNode
{
public int Data { get; set; }
public BinaryTreeNode leftChild { get; set; }
public BinaryTreeNode rightChild { get; set; } public BinaryTreeNode(int data)
{
this.Data = data;
} public BinaryTreeNode(int data, BinaryTreeNode left, BinaryTreeNode right)
{
this.Data = data;
this.leftChild = left;
this.rightChild = right;
}
}
二、解题思路
2.1 核心步骤
首先,通过下图了解遍历上图中的二叉树的过程:

通过上图可以总结出规律:
(1)当用前序遍历的方式访问到某一结点时,我们把该结点添加到路径上,并累加该结点的值。
(2)如果该结点为叶结点并且路径中结点值的和刚好等于输入的整数,则当前的路径符合要求,我们把它打印出来。如果当前结点不是叶结点,则继续访问它的子结点。
(3)当前结点访问结束后,递归函数将自动回到它的父结点。这里要注意的是:在函数退出之前要在路径上删除当前结点并减去当前结点的值,以确保返回父结点时路径刚好是从根结点到父结点的路径。
2.2 代码实现
public static void FindPath(BinaryTreeNode root, int expectedSum)
{
if (root == null)
{
return;
} int currentSum = ;
List<int> path = new List<int>();
FindPath(root, expectedSum, path, ref currentSum);
} private static void FindPath(BinaryTreeNode root, int expectedSum, List<int> path, ref int currentSum)
{
currentSum += root.Data;
path.Add(root.Data);
// 如果是叶结点,并且路径上结点的和等于输入的值
// 打印出这条路径
bool isLeaf = root.leftChild == null && root.rightChild == null;
if (isLeaf && currentSum == expectedSum)
{
foreach (int data in path)
{
Console.Write("{0}\t", data);
}
Console.WriteLine();
} // 如果不是叶结点,则遍历它的子结点
if (root.leftChild != null)
{
FindPath(root.leftChild, expectedSum, path, ref currentSum);
} if (root.rightChild != null)
{
FindPath(root.rightChild, expectedSum, path, ref currentSum);
} // 在返回到父结点之前,在路径上删除当前结点,
// 并在currentSum中减去当前结点的值
path.Remove(root.Data);
currentSum -= root.Data;
}
三、单元测试
3.1 测试用例
(1)辅助方法的封装
private static void TestPortal(string testName, BinaryTreeNode root, int expectedSum)
{
if (!string.IsNullOrEmpty(testName))
{
Console.WriteLine("{0} begins:", testName);
} FindPath(root, expectedSum); Console.WriteLine();
} private static void SetSubTreeNode(BinaryTreeNode root, BinaryTreeNode lChild, BinaryTreeNode rChild)
{
if (root == null)
{
return;
} root.leftChild = lChild;
root.rightChild = rChild;
} private static void ClearUpTreeNode(BinaryTreeNode root)
{
if (root != null)
{
BinaryTreeNode left = root.leftChild;
BinaryTreeNode right = root.rightChild; root = null; ClearUpTreeNode(left);
ClearUpTreeNode(right);
}
}
(2)功能、特殊输入测试
// 10
// / \
// 5 12
// /\
// 4 7
// 有两条路径上的结点和为22
public static void Test1()
{
BinaryTreeNode node10 = new BinaryTreeNode();
BinaryTreeNode node5 = new BinaryTreeNode();
BinaryTreeNode node12 = new BinaryTreeNode();
BinaryTreeNode node4 = new BinaryTreeNode();
BinaryTreeNode node7 = new BinaryTreeNode(); SetSubTreeNode(node10, node5, node12);
SetSubTreeNode(node5, node4, node7); Console.WriteLine("Two paths should be found in Test1.");
TestPortal("Test1", node10, ); ClearUpTreeNode(node10);
} // 10
// / \
// 5 12
// /\
// 4 7
// 没有路径上的结点和为15
public static void Test2()
{
BinaryTreeNode node10 = new BinaryTreeNode();
BinaryTreeNode node5 = new BinaryTreeNode();
BinaryTreeNode node12 = new BinaryTreeNode();
BinaryTreeNode node4 = new BinaryTreeNode();
BinaryTreeNode node7 = new BinaryTreeNode(); SetSubTreeNode(node10, node5, node12);
SetSubTreeNode(node5, node4, node7); Console.WriteLine("No paths should be found in Test2.");
TestPortal("Test2", node10, ); ClearUpTreeNode(node10);
} // 5
// /
// 4
// /
// 3
// /
// 2
// /
// 1
// 有一条路径上面的结点和为15
public static void Test3()
{
BinaryTreeNode node5 = new BinaryTreeNode();
BinaryTreeNode node4 = new BinaryTreeNode();
BinaryTreeNode node3 = new BinaryTreeNode();
BinaryTreeNode node2 = new BinaryTreeNode();
BinaryTreeNode node1 = new BinaryTreeNode(); node5.leftChild = node4;
node4.leftChild = node3;
node3.leftChild = node2;
node2.leftChild = node1; Console.WriteLine("One path should be found in Test3.");
TestPortal("Test3", node5, ); ClearUpTreeNode(node5);
} // 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
// 没有路径上面的结点和为16
public static void Test4()
{
BinaryTreeNode node1 = new BinaryTreeNode();
BinaryTreeNode node2 = new BinaryTreeNode();
BinaryTreeNode node3 = new BinaryTreeNode();
BinaryTreeNode node4 = new BinaryTreeNode();
BinaryTreeNode node5 = new BinaryTreeNode(); node1.leftChild = node2;
node2.leftChild = node3;
node3.leftChild = node4;
node4.leftChild = node5; Console.WriteLine("No paths should be found in Test4.");
TestPortal("Test4", node1, ); ClearUpTreeNode(node1);
} // 树中只有1个结点
public static void Test5()
{
BinaryTreeNode node1 = new BinaryTreeNode(); Console.WriteLine("One paths should be found in Test5.");
TestPortal("Test5", node1, ); ClearUpTreeNode(node1);
} // 树中没有结点
public static void Test6()
{
Console.WriteLine("No paths should be found in Test6.");
TestPortal("Test6", null, );
}
3.2 测试结果

剑指Offer面试题:23.二叉树中和为某一值的路径的更多相关文章
- 剑指Offer:面试题25——二叉树中和为某一值的路径(java实现)
问题描述: 输入一棵二叉树和一个整数,打印出二叉树中结点指的和为输入整数的所有路径.从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.二叉树结点的定义如下: public class Tree ...
- 剑指Offer - 九度1368 - 二叉树中和为某一值的路径
剑指Offer - 九度1368 - 二叉树中和为某一值的路径2013-11-23 03:46 题目描述: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结 ...
- 剑指offer(24)二叉树中和为某一值的路径
题目描述 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径 题目分析 这题基本上一看就知道应该深度遍历整个树, ...
- 【剑指Offer】 24、二叉树中和为某一值的路径
题目描述: 输入一颗二叉树的根结点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中, ...
- 【剑指Offer】24、二叉树中和为某一值的路径
题目描述 输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度大 ...
- 【剑指offer】Q25:二叉树中和为某一值的路径
说明:最烦的就是看别人的博客,题解里直接上代码,一行分析都没有.只是这个题... class BTNode(): def __init__(self, val = -1): self.val = va ...
- C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解
剑指offer 面试题23:从上往下打印二叉树 参与人数:4853 时间限制:1秒 空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...
- 【Offer】[34] 【二叉树中和为某一值的路径】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径.从树的根节点开始往下一直到叶节点所经过的节点形成一条路径.  ...
- 剑指Offer:面试题23——从上往下打印二叉树(java实现)
问题描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 按照层次遍历的方法,使用队列辅助. 1.将根结点加入队列. 2.循环出队,打印当前元素,若该结点有左子树,则将其加入队列,若 ...
随机推荐
- XSS之xssprotect(转)
参考资料 1 跨网站脚本 http://zh.wikipedia.org/wiki/XSS 2 http://code.google.com/p/xssprotect/ 一 跨网站脚本介绍 ...
- 高性能的JavaScript--数据访问(2)
动态作用域 无论是with表达式还是try-catch表达式的catch子句,以及包含()的函数,都被认为是动态作用域.一个动态作用域只因为代码运行而存在.因此无法通过静态分析(查看代码机构)来确定( ...
- 【Hibernate框架】关联映射(多对多关联映射)
按着我们的总结行进计划,接下来,就是有关于多对多映射的总结了. 我们来举个例子啊,很长时间以来,房价暴涨不落,但是还有很多人拥有很多套房产,假如说,一个富豪拥有九套房产,家里人么准去住哪一套,我们就以 ...
- dos下对mysql的简单操作(linux类似)
>>>>>>>>>>>>>>>>>>>> 基础入门语句10条 1. 连接服务器 ...
- 贝塞尔曲线(cubic bezier)
对于css3的Transitions,网上很多介绍,相信大家都比较了解,这里用最简单的方式介绍下: transition语法:transition:<transition-property> ...
- 如何动态在spring mvc中增加bean
阅读对象 搭框架人员,或者其他感兴趣的开发人员 背景 一般来说在业务代码中,加上 @Component, @Service,@Repository, @Controller等注解就可以实现将bean注 ...
- html 超文本标记语言
1.html超文本标记语言 2.在html中存在着大量的标签,我们用html中存在的标签将要显示在网页的内容包含起来. 3.css 控制网页显示内容的效果. 4.html+css 只能是静态网页. 5 ...
- selenium使用Xpath定位之完整篇
其中有一片文章提到了xpath元素定位,但是该文章中有些并不能适应一些特殊与个性化的场景.在文本中提供xpath元素的定位终极篇,你一定能在这里找到你需要的解决办法. 第一种方法: 通过绝对路径做定位 ...
- lua
lua的语言特性: 1. lua 的table可以实现多种数据结构:数组.记录.线性表.队列.集合等: 2. lua的closure闭合函数 3. lua的迭代器和泛型的for 4. lua的协同程序 ...
- 『AngularJS』$location 服务
项目中关于 $location的用法 简介 $location服务解析在浏览器地址栏中的URL(基于window.location)并且让URL在你的应用中可用.改变在地址栏中的URL会作用到$loc ...
