the longest distance of a binary tree
版权声明:欢迎查看本博客。希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735
the longest distance of a binary tree
个人信息:就读于燕大本科软件project专业 眼下大三;
本人博客:google搜索“cqs_2012”就可以;
个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;
博客内容:the longest distance of a binary tree;
博客时间:2014-4-26;
编程语言:C++ ;
编程坏境:Windows 7 专业版 x64;
编程工具:vs2008 32位编译器;
制图工具:office 2010 ppt;
硬件信息:7G-3 笔记本;
my words
simple life is just good.
words for my brother: i miss you
![]()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3FzX2V4cGVyaW1lbnQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
problem
the longest distance of a binary tree(problem from beauty of programming)
eg: the following binary tree
![]()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3FzX2V4cGVyaW1lbnQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
the longest distance is 8
my solution
recursion solution
int _Longest_distance(node * T,int & deep)
{
if(T == NULL)
{
deep = 0;
return 0;
}
else
{
int left = 0;
int right = 0;
int Ldeep = 0;
int Rdeep = 0; left = _Longest_distance(T->left,Ldeep);
right = _Longest_distance(T->right,Rdeep);
deep = (Ldeep > Rdeep? Ldeep:Rdeep) + 1;
int result = left > right?left :right;
result = result> (Ldeep + Rdeep)? result:(Ldeep+Rdeep);
return result ;
}
}
experiment
![]()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3FzX2V4cGVyaW1lbnQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
program run out: 7
my code
test.cpp
#include<iostream>
using namespace std; class node
{
public:
int data ;
node * left ;
node * right ;
node()
{
data = 0 ;
left = right = NULL ;
}
}; void _MakeTree(node * &T,int *data,int length);
void _Insert(node * & T,int data); void _MakeTree(node * &T,int *data,int length)
{
for(int i=0;i<length;i++)
{
_Insert(T,data[i]);
}
} void _Insert(node * & T,int data)
{
if(T == NULL)
{
T = new node();
T -> data = data;
}
else
{
node * p = T;
while(p != NULL)
{
if(data == p->data )
break;
else if(data < p->data )
{
if(p->left != NULL)
{
p = p ->left;
}
else{
p->left = new node();
(p->left) ->data = data;
break;
}
}
else{
if(p->right != NULL)
p = p->right;
else{
p->right = new node() ;
(p->right) ->data = data ;
break ;
}
}
}
}
} int _Longest_distance(node * T,int & deep)
{
if(T == NULL)
{
deep = 0;
return 0;
}
else
{
int left = 0;
int right = 0;
int Ldeep = 0;
int Rdeep = 0; left = _Longest_distance(T->left,Ldeep);
right = _Longest_distance(T->right,Rdeep);
deep = (Ldeep > Rdeep? Ldeep:Rdeep) + 1;
int result = left > right?left :right;
result = result> (Ldeep + Rdeep)? result:(Ldeep+Rdeep);
return result ;
}
} int main()
{
int data[] = {8,6,3,7,2,1,13,12,15,17}; node * T = NULL;
_MakeTree(T,data,10);
int deep =0;
cout<<_Longest_distance(T,deep)<<endl;
system("pause");
return 0;
}
the longest distance of a binary tree的更多相关文章
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- [LeetCode] All Nodes Distance K in Binary Tree 二叉树距离为K的所有结点
We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...
- [Swift]LeetCode863. 二叉树中所有距离为 K 的结点 | All Nodes Distance K in Binary Tree
We are given a binary tree (with root node root), a targetnode, and an integer value K. Return a lis ...
- 863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点
[抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Retur ...
- LeetCode – All Nodes Distance K in Binary Tree
We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...
- leetcode 863. All Nodes Distance K in Binary Tree
We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...
- [LC] 863. All Nodes Distance K in Binary Tree
We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...
- 863. All Nodes Distance K in Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 距离为K的节点 All Nodes Distance K in Binary Tree
2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...
随机推荐
- C#单元测试:NUnit详细使用方法
1. TDD的简介 首先什么是TDD呢?Kent Beck在他的<<测试驱动开发 >>(Addison-Wesley Professional,2003)一书中,使用下面2个原 ...
- 五、curator recipes之选举主节点Leader Latch
简介 在分布式计算中,主节点选举是为了把某个进程作为主节点来控制其它节点的过程.在选举结束之前,我们不知道哪个节点会成为主节点.curator对于主节点选举有两种实现方式,本文示例演示Latch的实现 ...
- ORACLE-DataGuard-重启服务器的方法
DG原理:主机向备机传送日志文件,备机执行日志文件,借此与主机数据同步. 依此原理,不难推导出DG的开关机顺序 关机顺序 先主机再备机,这样日志就不会断了 开机顺序 先备机再主机 ,这样的目标也是日 ...
- js 防止连续点击
简称 js防连点 var flag = true; $(".yzm>span").click(function(){ if(!flag){ return fals ...
- VC+++ 操作word
最近完成了一个使用VC++ 操作word生成扫描报告的功能,在这里将过程记录下来,开发环境为visual studio 2008 导入接口 首先在创建的MFC项目中引入word相关组件 右键点击 项目 ...
- BZOJ3625: 小朋友和二叉树
传送门 Sol 设 \(f_x\) 表示权值为 \(x\) 的二叉树的个数 设 \(s_x\) 表示是否有 \(x\) 这种权值可以选择 那么 \[f_n=\sum_{i=0}^{n}\sum_{j= ...
- JavaScript周报#183
This week’s JavaScript news Read this issue on the Web | Issue Archive JavaScript Weekly Issue 183Ma ...
- Ubuntu install TensorFlow 1.10 + CUDA 9.2 + cuDNN 7.2
为了装TensorFlow 1.10 下面升级一下系统的软件环境 NVIDIA DRIVER 去官网下载最新的linux驱动 http://www.nvidia.com/Download/in ...
- iphone崩溃日志分析工具symbolicatecrash用法
Symbolicatecrash是Xcode自带的一个分析工具,可以通过机器上的崩溃日志和应用的.dSYM文件定位发生崩溃的位置,把crash日志中的地址替换成代码相应位置. 使用效果: 分析前: T ...
- this keyword details
学生类: package com.itheima_07; /* * 学生类 * * 起名字我们要求做到见名知意. * 而我们现在的代码中的n和a就没有做到见名知意,所以我要改进. * * 如果有局部变 ...