题目大意:一颗二叉树,每个节点都有一个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. Apache Traffic Server(ats)

    零.前言1.官网 http://trafficserver.apache.org/2.国内社区 https://blog.zymlinux.net3.简洁明了的配置:http://blog.csdn. ...

  2. oracle数据库使用三个月的总结

    存储过程定义,举个例子如下: create or replace procedure test_person(id in Number, Ename In Varchar2, age In Varch ...

  3. HTML第二本书学习后记

    1,换行标记<br>,不换行标记<nobr></nobr> 2,添加水平线 <hr> 3,插入特殊符号:   空格 &nbsp    " ...

  4. JSON例子异常分析

    今天自己写了一个JSON的例子,可以一调用就出了问题,报下面这个异常: Java.lang.ClassNotFoundException: org.apache.commons.lang.except ...

  5. Spring 核心框架体系结构

    转载:http://www.admin10000.com/document/10447.html 很多人都在用spring开发java项目,但是配置maven依赖的时候并不能明确要配置哪些spring ...

  6. 使用openvswitch 和dnsmasq来实现虚拟机网络隔离

    openvswicth : 开源的网络虚拟化软件,可以划分vlan隔离虚拟机,做流量控制 dnsmasq:小心的dns,dhcp服务器 安装openvswicth wget  http://openv ...

  7. iOS程序中的内存分配 栈区堆区全局区

    在计算机系统中,运行的应用程序的数据都是保存在内存中的,不同类型的数据,保存的内存区域不同.一.内存分区 栈区(stack) 由编译器自动分配并释放,存放函数的参数值,局部变量等.栈是系统数据结构,对 ...

  8. Spring boot mybatis项目启动后一直刷日志的bug修复……

    最近接手一个项目,使用的框架是springboot+mybatis: 其中持久层是使用mybatis集成的,sql是配置在mapper.xml文件中: 然后呢,有时候做新功能的时候,往xml文件中增加 ...

  9. Hibernate操作指南-搭建一个简单的示例(基于原生API和XML)

  10. 如果空间不够的话,iOS发生这样的错误

    2016-12-16 10:24:50.945 gpxj[2634:21323] Simulator user has requested new graphics quality: 10 2016- ...