【二叉树的递归】03判断二叉树中有没有和为给定值的路径【Path Sum】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
给定一个二叉树和一个和,判断这个树中是否有一个从根到叶子的路径,使其这个路径上面的所有节点值的和为这个给定的值。
例如:
给定下面的二叉树,并且和为22。
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
返回true,因为这里面存在一个根到叶子的路径 5->4->11->2,使其他们的和为22。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test.cpp:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#include <iostream>
#include <cstdio> #include <stack> #include <vector> #include <queue> #include "BinaryTree.h" using namespace std; bool findPathSum(TreeNode *root, vector<int> &path, int sum) // 树中结点含有分叉, ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3); bool ans = hasPathSum(pNodeA1, 20); if (ans == true) DestroyTree(pNodeA1); |
结果输出:
exist!
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_ struct TreeNode TreeNode *CreateBinaryTreeNode(int value); #endif /*_BINARY_TREE_H_*/ |
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
#include <iostream>
#include <cstdio> #include "BinaryTree.h" using namespace std; /** //创建结点 return pNode; //连接结点 //打印节点内容以及左右子结点内容 if(pNode->left != NULL) if(pNode->right != NULL) printf("\n"); //前序遍历递归方法打印结点内容 if(pRoot != NULL) if(pRoot->right != NULL) void DestroyTree(TreeNode *pRoot) delete pRoot; DestroyTree(pLeft); |
【二叉树的递归】03判断二叉树中有没有和为给定值的路径【Path Sum】的更多相关文章
- 【二叉树的递归】05二叉树中找任意起点和终点使他们的路径和最大【Binary Tree Maximum Path Sum】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,寻找值最大的路径. ...
- 【二叉树的递归】02二叉树的最大深度【Maximum Depth of Binary Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...
- 【二叉树的递归】01二叉树的最小深度【Minimum Depth of Binary Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...
- 【剑指offer】判断二叉树是否为平衡二叉树
2013-09-03 14:16:51 面试题39:求二叉树的深度.判断二叉树是否为平衡二叉树 小结: 根据平衡二叉树的定义,需要判断每个结点,因此,需要遍历二叉树的所有结点,并判断以当前结点为根的树 ...
- 5分钟了解二叉树之LeetCode里的二叉树
有读者反馈,现在谁不是为了找工作才学的数据结构,确实很有道理,是我肤浅了.所以为了满足大家的需求,这里总结下LeetCode里的数据结构.对于我们这种职场老人来说,刷LeetCode会遇到个很尴尬的问 ...
- 数据结构(3) 第三天 栈的应用:就近匹配/中缀表达式转后缀表达式 、树/二叉树的概念、二叉树的递归与非递归遍历(DLR LDR LRD)、递归求叶子节点数目/二叉树高度/二叉树拷贝和释放
01 上节课回顾 受限的线性表 栈和队列的链式存储其实就是链表 但是不能任意操作 所以叫受限的线性表 02 栈的应用_就近匹配 案例1就近匹配: #include <stdio.h> in ...
- 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java
前一段时间,学习数据结构的各种算法,概念不难理解,只是被C++的指针给弄的犯糊涂,于是用java,web,javascript,分别去实现数据结构的各种算法. 二叉树的遍历,本分享只是以二叉树中的先序 ...
- 【二叉树的递归】04找出二叉树中路径和等于给定值的所有路径【Path Sum II】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...
- 用c语言实现前序创建二叉树(递归),分别用前序,中序,后序遍历,以及分别输出节点个数和叶子节点个数
本人c语言小白一枚,近期在学习数据结构(c语言版),特写此随笔,做一些总结和分享,如有不当之处,请各位技术大牛斧正 首先我们用一个结构体来抽象树的结点,代码如下(这里我们存放的数据为char型,大家可 ...
随机推荐
- VS使用WEB DEPLOY发布
背景是这样的,公司有两台服务器,平时一台备用,另一台做为主生产机器.当有大量补丁或者安装什么东西需要重启的时候,交其中一台直接关掉IIS,然后重启即可,此时另一台负责处理用户请求. 之前一台服务器一个 ...
- Cassandra数据库Java訪问
针对的时Cassandra 2.0 数据库 Java本地client訪问Cassandra,首先建立Javaproject,使用Maven进行管理. 引入依赖: <dependency> ...
- sublime text 3 语法检查插件
第一种方法:有点卡 先去下载对应的开发环境,安装到本地,例如php. 从Pakage Control中安装sublimelinter和sublimelinter-*,*为所用的语言,例如sublime ...
- JSP 分页代码
jsp 分页模板 后台分页代码: 说明: 在 com.zc.domain 包下: PageBean.java 文件 package cn.itcast.customer.domain; impor ...
- Docker dubbo 服务注册
vim run.sh #!/bin/baship=`ifconfig eth0 |grep "inet"|awk '{print $2}'`hn=dubbo-service-pro ...
- UI组件之Label
Use Core Data 接口,链接数据库 Portrait 肖像模式 LandScape(Left, Right) 风景模式 1.程序启动后,从main接口进入, main函数会调用UIAppli ...
- hadoop 根据secondary namenode恢复namenode
refer to http://www.cnblogs.com/Richardzhu/p/3435989.html http://blog.csdn.net/wuzhilon88/article/de ...
- android客户端登录&注册的实现
MainActivity多线程的实现: package com.example.loginconnect; import java.lang.ref.WeakReference; import jav ...
- 【LeetCode】:二叉树的Max,Min深度
一.最大深度问题 描述: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes a ...
- 图形用户界面(GUI)事件监听机制——窗体事件Frame
窗体事件.Button的使用 本事例给出一个窗体的定义方法,基本属性设置,给窗体添加退出按钮,让按钮具备退出的功能.熟悉监听器的使用 按钮就是事件源. 那么选择哪一个监听器呢? 通过关闭窗体事例了解到 ...