<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 ...
随机推荐
- [计算机故障]windows8+IE11显示无法登陆网页
同事的一款SURFACE,跑的WINDOWS 8.1.最近发现从开始菜单里的win8.1的浏览器可以上网,但是通过任务栏的快捷方式,又上不了网.尝试重置了IE11,安装了Google Chrome.但 ...
- Return Largest Numbers in Arrays
题目要求 右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组. 提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组的每个元素. 答题思路 ...
- android逆向代码分析截图
- dragdrop 修改版
<!DOCTYPE html> <html lang="en"> <head> <meta name="keywords&quo ...
- 关于apache做301的问题
http://www.internetmarketingninjas.com/blog/search-engine-optimization/301-redirects/ RedirectMatch ...
- web api与webservice(转)
webservice走HTTP协议和80端口而你说的api,用的协议和端口,是根据开发人员定义的 这么说吧,api类似于cs架构,需要同时开发客户端API和服务器端程序而WebService则类似于b ...
- Flash+fms视频录制在项目中的实际应用
Flash+fms视频录制在项目中的实际应用 前言:以下只是记录本人在项目中的应用,而flash+fms视频录制有多种实现方式,具体可根据实际情况而定! 1:古人云:工欲善其事,必先利其器,首先安装f ...
- python引用py文件中文报错
文件 a.py 中引用文件 b.py 如果文件b.py中包含中文,会报错. 文件hello.py中代码如下: def say_nihao(): print "你好" 文件main. ...
- IntelliMVCCode智能MVC架构的代码助手使用方法
智能代码生成工具,快速帮助开发者提升开发速度,通过工具自动生成MVC架构的大量源代码,节省更多的开发时间. 工具使用的框架:.net4.0,通过工具连接到数据库自动提取数据表或视图中的结构,生成对应的 ...
- MWeb 2.0.7 版发布!
更新前针对 MAS 上的评论重点说一下:MWeb 是支持直接对本地文件夹操作的,不用导入到文档库!请使用外部模式!请使用外部模式!!请使用外部模式!!! 重要的话讲三次!使用方法是 CMD + E 打 ...