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进行层次遍历即可. 这种解法 ...
随机推荐
- 关于service相关知识的认识
做android的程序开发也有了许久了,当做一个大程序的时候,越来越发现service是非常有用的方法,当你想后台运行数据或者音乐播放操作的时候,都可以才有service,根据实际情况,写成local ...
- libevent学习笔记 —— 第一个程序:计时器
用libevent写个定时器其实步骤不多: 1.初始化libevent 2.设置事件 3.添加事件 4.进入循环 由于定时事件触发之后,默认自动删除,所以如果要一直计时,则要在回调函数中重新添加定时事 ...
- 《JavaWeb从入门到改行》很好的复习资料: SQL语句到底怎么写 ?
本文用到的数据库如下: CREATE DATABASE exam; /创建部门表/ CREATE TABLE dept( deptno INT PRIMARY KEY, dname ), loc ) ...
- python学习之老男孩python全栈第九期_day019知识点总结——collections模块、时间模块、random模块、os模块、sys模块
一. collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:namedtuple.deque.Counte ...
- 由上一个血案引发的关于property和attribute关系的思考
boss说,学习要刨根问底. 好的,开刨. 一.property和attribute在英语里有什么区别 看似没有区别.但其实大神说: property是 物体本身自带属性,不能改变的(一旦改了就是另外 ...
- USACO08MAR土地购买 与 APIO2010特别行动队
两道斜率优化DP: 土地购买 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地.如果约翰单买一块土 地,价格就是土地的面积.但他可以选择并购一组土地,并购的价格为这些土地中最大的长 乘以最大的 ...
- JavaScript - 收藏集 - 掘金
Angular 中的响应式编程 -- 浅淡 Rx 的流式思维 - 掘金第一节:初识Angular-CLI第二节:登录组件的构建第三节:建立一个待办事项应用第四节:进化!模块化你的应用第五节:多用户版本 ...
- 《Linux命令行与Shell脚本编程大全第2版》读书笔记
公司说不准用云笔记了,吓得我赶紧把笔记贴到博客上先..... 近3年前的了,只有一半的章节,后面的没空记录了.... 第1章 可以cat /proc/meminfo文件来观察Linux系统上虚拟内存的 ...
- npm 更新至最新版本
有时npm版本低了,一些操作有问题,要更新成最新. 官网中: https://www.npmjs.com/get-npm 先查看对应的 node 版本 和 npm版本 Check that ...
- Codeforces Round #417 B. Sagheer, the Hausmeister
B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes Som ...