《剑指offer》第三十四题(二叉树中和为某一值的路径)
// 面试题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》第三十四题(二叉树中和为某一值的路径)的更多相关文章
- (剑指Offer)面试题25:二叉树中和为某一值的路径
题目: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 二叉树结点的定义: struct TreeNode ...
- 【剑指offer】面试题25:二叉树中和为某一值的路径
题目: 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 思路: dfs一下就可以了.一般dfs肯定递归写比 ...
- 《剑指offer》— JavaScript(24)二叉树中和为某一值的路径
二叉树中和为某一值的路径 题目描述 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 思路 前序遍历二叉树, ...
- 剑指Offer(三十四):第一个只出现一次的字符
剑指Offer(三十四):第一个只出现一次的字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...
- 《剑指offer》第二十四题(反转链表)
// 面试题24:反转链表 // 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的 // 头结点. #include <iostream> #include &quo ...
- 《剑指offer》第十四题(剪绳子)
// 面试题:剪绳子 // 题目:给你一根长度为n绳子,请把绳子剪成m段(m.n都是整数,n>1并且m≥1). // 每段的绳子的长度记为k[0].k[1].…….k[m].k[0]*k[1]* ...
- 剑指offer二十四之二叉树中和为某一值的路径
一.题目 输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径. 二.思路 详见代码 三.代码 1.解答代码 im ...
- 剑指Offer(三十二):把数组排成最小的数
剑指Offer(三十二):把数组排成最小的数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/b ...
- 剑指Offer(三十六):两个链表的第一个公共结点
剑指Offer(三十六):两个链表的第一个公共结点 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...
- 剑指Offer(三十五):数组中的逆序对
剑指Offer(三十五):数组中的逆序对 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/bai ...
随机推荐
- Redis演示及使用场景
概述 Redis是一个开源的.使用C语言编写的.支持网络交互的.可基于内存也可持久化的Key-Value(字典, Remote Dictionary Server,远程字典服务器)数据库. 客户端:h ...
- Summary: difference between public, default, protected, and private key words
According to Java Tutorial: Controlling Access to Members of a Class Access level modifiers determin ...
- #C++初学记录(算法2)
A - Game 23 Polycarp plays "Game 23". Initially he has a number n and his goal is to trans ...
- Lower Power with CPF(四)
CPF从Front-end到Back-end(RTL--GDSII)的整个流程: 1)Creating a CPF file:来在前端就建立lower power的规范. 2)检查CPF文件的正确性, ...
- 模仿WIN32程序处理消息
#include "stdafx.h" #include "MyMessage.h" #include <conio.h> using namesp ...
- js文本框字数限制
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Cookie学习笔记
1.简介 1.什么是cookie:cookie是一种能够让网站服务器把少量数据(4kb左右)存储到客户端的硬盘或内存.并且读可以取出来的一种技术. 2.当你浏览某网站时,由web服务器放置于你硬盘上的 ...
- python 单例模式,一个类只能生成唯一的一个实例,重写__new__方法详解
单例:一个类只能生成唯一的一个实例 每个类只要被实例化了,他的私有属性 '_instance'就会被赋值,这样理解对吗 对 #方法1,实现__new__方法 #并在将一个类的实例绑定到类变量_inst ...
- 单片机裸机下写一个自己的shell调试器(转)
源: 单片机裸机下写一个自己的shell调试器
- Kafka学习之(七)搭建kafka可视化服务Kafka Eagle
一.下载安装包 kafka-eagle-bin-1.2.4.tar.gz 百度云链接:链接:https://pan.baidu.com/s/1SNIkpsvs20A_Ljtx5PaMuA 密码:o4 ...