// 面试题34:二叉树中和为某一值的路径
// 题目:输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所
// 有路径。从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。 #include <iostream>
#include "BinaryTree.h"
#include <vector>
using namespace std; void FindPath(BinaryTreeNode* pRoot, int expectedSum, std::vector<int>& path, int& currentSum); void FindPath(BinaryTreeNode* pRoot, int expectedSum)
{
if (pRoot == nullptr)
return; std::vector<int> path;
int currentSum = ;
FindPath(pRoot, expectedSum, path, currentSum);
} void FindPath
(
BinaryTreeNode* pRoot,
int expectedSum,
std::vector<int>& path,
int& currentSum
)
{
currentSum += pRoot->m_nValue;
path.push_back(pRoot->m_nValue); // 如果是叶结点,并且路径上结点的和等于输入的值
// 打印出这条路径
bool isLeaf = (pRoot->m_pLeft == nullptr && pRoot->m_pRight == nullptr);
if (currentSum == expectedSum && isLeaf)
{
printf("A path is found: ");
vector<int>::iterator iter = path.begin();//使用迭代器而不是下标
for (; iter != path.end(); ++iter)
printf("%d\t", *iter); printf("\n");
} // 如果不是叶结点,则遍历它的子结点,注意是遍历
if (pRoot->m_pLeft != nullptr)
FindPath(pRoot->m_pLeft, expectedSum, path, currentSum);
if (pRoot->m_pRight != nullptr)
FindPath(pRoot->m_pRight, expectedSum, path, currentSum); // 在返回到父结点之前,在路径上删除当前结点,
// 并在currentSum中减去当前结点的值
currentSum -= pRoot->m_nValue;
path.pop_back();
} // ====================测试代码====================
void Test(const char* testName, BinaryTreeNode* pRoot, int expectedSum)
{
if (testName != nullptr)
printf("%s begins:\n", testName); FindPath(pRoot, expectedSum); printf("\n");
} // 10
// / \
// 5 12
// /\
// 4 7
// 有两条路径上的结点和为22
void Test1()
{
BinaryTreeNode* pNode10 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode12 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode10, pNode5, pNode12);
ConnectTreeNodes(pNode5, pNode4, pNode7); printf("Two paths should be found in Test1.\n");
Test("Test1", pNode10, ); DestroyTree(pNode10);
} // 10
// / \
// 5 12
// /\
// 4 7
// 没有路径上的结点和为15
void Test2()
{
BinaryTreeNode* pNode10 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode12 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode10, pNode5, pNode12);
ConnectTreeNodes(pNode5, pNode4, pNode7); printf("No paths should be found in Test2.\n");
Test("Test2", pNode10, ); DestroyTree(pNode10);
} // 5
// /
// 4
// /
// 3
// /
// 2
// /
// 1
// 有一条路径上面的结点和为15
void Test3()
{
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode5, pNode4, nullptr);
ConnectTreeNodes(pNode4, pNode3, nullptr);
ConnectTreeNodes(pNode3, pNode2, nullptr);
ConnectTreeNodes(pNode2, pNode1, nullptr); printf("One path should be found in Test3.\n");
Test("Test3", pNode5, ); DestroyTree(pNode5);
} // 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
// 没有路径上面的结点和为16
void Test4()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode1, nullptr, pNode2);
ConnectTreeNodes(pNode2, nullptr, pNode3);
ConnectTreeNodes(pNode3, nullptr, pNode4);
ConnectTreeNodes(pNode4, nullptr, pNode5); printf("No paths should be found in Test4.\n");
Test("Test4", pNode1, ); DestroyTree(pNode1);
} // 树中只有1个结点
void Test5()
{
BinaryTreeNode* pNode1 = CreateBinaryTreeNode(); printf("One path should be found in Test5.\n");
Test("Test5", pNode1, ); DestroyTree(pNode1);
} // 树中没有结点
void Test6()
{
printf("No paths should be found in Test6.\n");
Test("Test6", nullptr, );
} int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
system("pause");
return ;
}

《剑指offer》第三十四题(二叉树中和为某一值的路径)的更多相关文章

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

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

  2. 【剑指offer】面试题25:二叉树中和为某一值的路径

    题目: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 思路: dfs一下就可以了.一般dfs肯定递归写比 ...

  3. 《剑指offer》— JavaScript(24)二叉树中和为某一值的路径

    二叉树中和为某一值的路径 题目描述 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 思路 前序遍历二叉树, ...

  4. 剑指Offer(三十四):第一个只出现一次的字符

    剑指Offer(三十四):第一个只出现一次的字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...

  5. 《剑指offer》第二十四题(反转链表)

    // 面试题24:反转链表 // 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的 // 头结点. #include <iostream> #include &quo ...

  6. 《剑指offer》第十四题(剪绳子)

    // 面试题:剪绳子 // 题目:给你一根长度为n绳子,请把绳子剪成m段(m.n都是整数,n>1并且m≥1). // 每段的绳子的长度记为k[0].k[1].…….k[m].k[0]*k[1]* ...

  7. 剑指offer二十四之二叉树中和为某一值的路径

    一.题目 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 二.思路 详见代码 三.代码 1.解答代码 im ...

  8. 剑指Offer(三十二):把数组排成最小的数

    剑指Offer(三十二):把数组排成最小的数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/b ...

  9. 剑指Offer(三十六):两个链表的第一个公共结点

    剑指Offer(三十六):两个链表的第一个公共结点 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...

  10. 剑指Offer(三十五):数组中的逆序对

    剑指Offer(三十五):数组中的逆序对 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/bai ...

随机推荐

  1. testng入门教程1在testng运行一个简单的testcase

    在eclips运行java,创建一个Java类文件名TestNGSimpleTest  C:\ > TestNG_WORKSPACE import org.testng.annotations. ...

  2. Qt计时器

    在Qt中使用定时器有两种方法,一种是使用QObiect类的定时器:一种是使用QTimer类.定时器的精确性依赖于操作系统和硬件,大多数平台支持20ms的精确度. ■.QObject类的定时器QObje ...

  3. 开发安卓安装流程(codorva+ionic)

    开发安卓安装流程 0 安装操作系统  Win10   用户名称尽量英文字母加数字,避免编码问题 1 安装Java sdk 1.8.0_45   所需文件 jdk-8u45-windows-x64 1. ...

  4. 使用 amcharts 和 highcharts 绘制多曲线时间趋势图的通用方法

    工作中用到, 这里分享一下. 可以使用 amcharts 和 highcharts 在同一坐标中绘制多个对比曲线图. 当然, 对图形没有过多装饰, 可以参考 API 文档: highcharts:   ...

  5. python isinstance用法

    isinstance(object,type) 其第一个参数(object)为对象,第二个参数(type)为类型名(int...)或类型名的一个列表((int,list,float)是一个列表). 其 ...

  6. Python: 没有switch-case语句

    初学Python语言,竟然很久才发现Python没有switch-case语句 官方的解释说,“用if... elif... elif... else序列很容易来实现 switch / case 语句 ...

  7. Python: 在序列上执行聚集函数(比如sum() , min() , max() )

    在序列上执行聚集函数(比如sum() , min() , max() ) eg1: >>>nums = [1, 2, 3, 4, 5]>>>s = sum(x * ...

  8. linux下删除大量文件提示参数过长解决办法

    linux下删除大量文件提示参数过长解决办法:在当前目录下rm -rf * 在linux中删除大量文件时,直接用rm会出现:-bash: /bin/rm: 参数列表过长的错误. 这时可以用find命令 ...

  9. Linux服务器---apache配置文件

    Apache配置文件 Apache的配置文件默认路径是“/etc/httpd/conf/httpd.conf”,编辑该文件就可以修改Apache的配置 1.设置网页主目录,参数DocumentRoot ...

  10. 怎么说, 开发会很乐意去主动修改bug?

    怎么说, 开发会很乐意去主动修改bug? 一图顶上千言万语,如下: