题目大意:一颗二叉树,每个节点都有一个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. ecmall源码中的 function _config_view()-关于重写

    ecmall中类库的继承过程: defaultAPP->MallbaseApp->FrontendApp->EcBaseApp->BaseApp->Object 在bas ...

  2. Ubuntu apt-get "Hash Sum mismatch" 问题解决方法

    参考:ubuntu: apt-get update的时候遇到"Hash Sum mismatch"错误 在安装Mininet的时候,apt-get update的时候遇到了这个问题 ...

  3. vpn

    https://itunes.apple.com/us/app/sonicwall-mobile-connect/id822514576?mt=12

  4. C中嵌入python

    嵌入 与python的扩展相对,嵌入是把Python解释器包装到C的程序中.这样做可以给大型的,单一的,要求严格的,私有的并且(或者)极其重要的应用程序内嵌Python解释器的能力.一旦内嵌了Pyth ...

  5. How to install starDIct on suse OS?

    1. Access page http://code.google.com/p/stardict-3/ to download starDict package or use zypper in to ...

  6. +Load和+initialize方法解析

    http://www.cnblogs.com/ider/archive/2012/09/29/objective_c_load_vs_initialize.html

  7. C# 日期格式转【转】

    使用:DateTime.ToString的方法(String, IFormatProvider)转换格式 using System; using System.Globalization; Strin ...

  8. hosts的设置,我们直接可以在下面路径找到hosts文件:C:\Windows\System32\drivers\etc

    hosts的设置,我们直接可以在下面路径找到hosts文件:C:\Windows\System32\drivers\etc

  9. AngularJS基础知识1

    一.angularJS简介 1.什么是 AngularJS? AngularJS 是一个 JavaScript 框架.它是一个以 JavaScript 编写的库.AngularJS是协助搭建单页面工程 ...

  10. DEDE有无缩略图如何调取

    同一样式分开调取 [field:array runphp='yes']@me = (strpos(@me['litpic'],'defaultpic') ? "":"&l ...