版权声明:欢迎查看本博客。希望对你有有所帮助 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的更多相关文章

  1. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. 863. All Nodes Distance K in Binary Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. 距离为K的节点 All Nodes Distance K in Binary Tree

    2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...

随机推荐

  1. MUI框架 picker日期选择器实例

    MUI官方文档点我 (一)准备工作,下载相关的js.cs文件,地址 (二)新建普通html页面 1)引入相关js.cs文件 2) 一个input,记录下id: <form> <lab ...

  2. 腾讯云安全组,实现服务器外网ip访问网站

    添加访问地ip出入站规则HTTP,并将云主机添加到安全组中.

  3. HDU 1874(简单最短路) (大优化)

    优先队列那里用greater会报错 http://acm.hdu.edu.cn/showproblem.php?pid=1874 /* 使用pair代替结构 */ #include <iostr ...

  4. 原型模式(GOF23)

    依赖关系的倒置 基本假设在于抽象变化的慢,而依赖于抽象的细节变化的快,所以要做到抽象不依赖于实现的细节,而实现细节应该依赖于抽象 设计模式不是代码的复用,而是经验的复用,通过分析来定义抽象和细节,不要 ...

  5. 理解webpack4.splitChunks之cacheGroups

    cacheGroups其实是splitChunks里面最核心的配置,一开始我还认为cacheGroups是可有可无的,这是完全错误的,splitChunks就是根据cacheGroups去拆分模块的, ...

  6. MSSQL中的表变量

    最近在看<Microsoft SQL Server2005技术内幕:T-SQL程序设计> 1.表变量的事务上下文中提到,表变量不受外部事务回滚影响. 举个例子: DECLARE  @TA ...

  7. CentOS7运维管理笔记(12)----修改主机名

    CentOS修改主机名 CentOS7和CentOS6.5 修改主机名的方法略有不同. 通过 hostname 命令可以查看当前的主机名. 1. 临时修改主机名 通过 'hostname 新的主机名' ...

  8. (WPF) Textbox 每次输入触发事件处理

    1.  Code behind. protected void TextBox1_TextChanged(object sender, EventArgs e) { Label1.Text = Ser ...

  9. RocketMQ读书笔记6——可靠性优先的使用场景

    [顺序消息] 顺序消费是指消息的产生顺序和消费顺序相同. 比如订单的生成.付款.发货,这三个消息必须按顺序处理才可以. [顺序消息的分类] 全局顺序消息和部分顺序消息. 上面订单的例子,其实是部分顺序 ...

  10. 解释型vs编译型 动态vs静态 强类型vs弱类型

    ------------------------------------------------------------ 释型.动态语言与静态语言.强类型语言与弱类型语言的区别 编译型和解释型 我们先 ...