题目大意:一颗二叉树,每个节点都有一个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>二叉树根到叶节点求和值匹配的更多相关文章

  1. [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 ...

  2. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

    Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...

  3. [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 ...

  4. C语言递归之求根到叶节点数字之和

    题目描述 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点 ...

  5. [二叉树-根到叶的子路径]路径总和 III (两层递归)

    题目437. 路径总和 III 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父 ...

  6. java 遍历树节点 同时保留所有的从根到叶节点的路径

    直接在代码.稍后细说 数据结构定义: /** * */ package Servlet; import java.util.ArrayList; import java.util.List; /** ...

  7. 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 ...

  8. 树——sum-root-to-leaf-numbers(根到叶节点数字之和)

    问题: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a numb ...

  9. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

随机推荐

  1. ASM FailGroup验证

    ASM-FailGroup验证 一.FailGroup有效性验证 创建DiskGroup,在Redundancy选项 High:至少3块disk,至少3个failgroup,每一个extent存在1主 ...

  2. git 常见问题

    RPC failed; error: RPC failed; curl 56 SSL read: error:00000000:lib(0):func(0):reason(0), er rno 100 ...

  3. MySQL(无GUI) Windows安装和启动

    1.在官网http://dev.mysql.com/downloads/下载 MySQL Community Server 2.解压后是这个样子: 3.找到cmd,并且用管理员权限启动,必须!不然那没 ...

  4. mysql慢查询日志分析工具mysqldumpslow

    一.mysqldumpslow为mysql自带,安装后既带有该工具. 二.mysqldumpslow经常使用的参数 -s,是order的顺序 al 平均锁定时间 ar 平均返回记录时间 at 平均查询 ...

  5. rabbitmq之消息重入队列

    说起消息重入队列还得从队列注册消费者说起,客户端在向队列注册消费者之后,创建的channel也会被主队列进程monitor,当channel挂掉后,主队列进程(rabbit_amqqueue_proc ...

  6. java常见面试题及答案 1-10(基础篇)

    java常见面试题及答案 1.什么是Java虚拟机?为什么Java被称作是"平台无关的编程语言"? Java 虚拟机是一个可以执行 Java 字节码的虚拟机进程.Java 源文件被 ...

  7. ACM集训的Day3 B。。。盲目搜索之DFS。。。

    milk 一.题目描述: gzp有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数, 最初,A和B桶都是空的,而C桶是装满牛奶的.有时,农民把牛奶从一个桶倒到 另一个桶中,直到被灌 ...

  8. java类初始化的过程

    在复习Thinking in java的过程中看到了相关内容,顺便整理一下,像下面一样的代码具体的执行顺序(ABCD都是类) public class A { public A(String text ...

  9. 给HashMap排序,使之成为有序Map

    如何给HashMap中的值排序?这个问题很多人都遇到过,很常见的一个方案是使用LinkedHashMap,因为LinkedHashMap可以记住元素放入的顺序,可以认为是真正的“有序”(想让HashM ...

  10. scrollViewDidEndScrollingAnimation和scrollViewDidEndDecelerating的区别

    #pragma mark - 监听 /**  *  点击了顶部的标题按钮  */ - (void)titleClick:(XMGTitleButton *)titleButton {     // 修 ...