<Interview Problem>二叉树根到叶节点求和值匹配
题目大意:一颗二叉树,每个节点都有一个Value, 判断根节点到叶节点的路径求和值是否等于某个数Sum.
比如说如下这样一颗二叉树,76是45,21,10这条路径的求和值,77就没有满足条件的路径。
45
21 65
10 24 50 70
代码依旧用C++来实现,二叉树一般采用递归的方式来解决。
#include <iostream>
using namespace std;
typedef struct BTree
{
int value;
struct BTree* left;
struct BTree* right;
} BTree;
typedef struct BTree Node;
//recursively insert a tree node
BTree* Insert(BTree* T, int value)
{
if(T == NULL)
{
T = (BTree*)malloc(sizeof(struct BTree));
if(T == NULL)
printf("Malloc failed");
else{
T->value = value;
T->left = T->right = NULL;
}
}
else if(value < T->value)
T->left = Insert(T->left,value);
else if(value > T->value)
T->right = Insert(T->right,value);
return T;
}
BTree* MakeEmpty(BTree* T)
{
if(T != NULL){
MakeEmpty(T->left);
MakeEmpty(T->right);
free(T);
}
return NULL;
}
bool hasPathSum( Node *node, int sum, int pathSum)
{
bool match = false;
if(node != NULL)
pathSum += node->value;
if(node->left== NULL && node->right == NULL)
{
if(sum == pathSum)
match = true;
else
match = false;
}
if(node->left != NULL && !match)
match = hasPathSum(node->left,sum,pathSum);
if(node->right != NULL && !match)
match = hasPathSum(node->right,sum,pathSum);
return match;
}
bool hasPathSum( Node *root, int sum)
{
if(root == NULL) return false;
bool match = false;
match = hasPathSum(root,sum,);
return match;
}
int main()
{
BTree* T = NULL;
T = Insert(T,);
T = Insert(T,);
T = Insert(T,);
T = Insert(T,);
T = Insert(T,);
T = Insert(T,);
T = Insert(T,);
bool match = hasPathSum(T,);
cout << match << endl;
match = hasPathSum(T,);
cout << match << endl;
MakeEmpty(T);
return ;
}
<Interview Problem>二叉树根到叶节点求和值匹配的更多相关文章
- [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- C语言递归之求根到叶节点数字之和
题目描述 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点 ...
- [二叉树-根到叶的子路径]路径总和 III (两层递归)
题目437. 路径总和 III 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父 ...
- java 遍历树节点 同时保留所有的从根到叶节点的路径
直接在代码.稍后细说 数据结构定义: /** * */ package Servlet; import java.util.ArrayList; import java.util.List; /** ...
- LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 树——sum-root-to-leaf-numbers(根到叶节点数字之和)
问题: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a numb ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- 百度地图api
引入js <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak ...
- 贪吃蛇的java代码分析(一)
自我审视 最近自己学习java已经有了一个多月的时间,从一开始对变量常量的概念一无所知,到现在能勉强写几个小程序玩玩,已经有了长足的进步.今天没有去学习,学校里要进行毕业答辩和拍毕业照了,于是请了几天 ...
- BT客户端实现 Peer协议设计
与peer建立tcp连接后,首先发送handshake消息进行握手 handshake消息格式如下: 一个字节0x19 + 一个字符串'BitTorrent protocol' + 8 byte 保留 ...
- Oracle实战训练——ATM取款机业务
ATM取款机的数据库模拟开发和实战总结 一.ATM实战开发的简介. 学习了几天的Oracle,开始着手用数据库PL/SQL语言做一个简单的ATM取款机业务,主要是为了巩固数据库的知识,并非真正的去实现 ...
- linux io stack
- Spring操作指南-AOP基本示例(基于XML)
- SQL索引添加
EXPLAIN select * from view_agzsaycommont where id >0
- delphi 弹出选择目录窗口
if not SelectDirectory( '请选择输出文件路径','/',directory) then begin Exit; end; 使用SelectDirectory函数注意要在use下 ...
- python中的GIL(全局解释锁)多线程能够提升效率
预启动的时候,应用程序仍然会调用 OnLaunched 方法的,在 OnLaunched 方法调用之后,会马上发生 Suspending 事件,随后应用就会暂停. 我先基于develop主分支拉出一个 ...
- AD中各层的说明
PCB各层说明: 1.丝印层(OverLay,Silkscreen):有顶层丝印和底层丝印.用来画器件轮廓,器件编号和一些图案等. 2.信号层(SignalLayer):对于两层板,主要是TopLay ...