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进行层次遍历即可. 这种解法 ...
随机推荐
- wpf 子窗体调用主窗体中函数的实现
子窗体(Win_Set): MainWindow m; m = (MainWindow)this.Owner; m.showText(&qu ...
- 撩课-每天刷Web面试题(前10天汇总)-Day12
一.算法题部分 1. 如何获取浏览器URL中查询字符串中的参数 function getParamsWithUrl(url) { var args = url.split('?'); ] === ur ...
- FisherYates费雪耶兹随机置乱算法
public class FisherYates { public static void main(String[] args) { int[] arr = new int[10]; // 初始有序 ...
- UVA401(回文子串和镜像串)
可以先设一个常量镜像串,直接返回比较 #include <iostream> #include <string> #include <cstring> #inclu ...
- Spring与Web整合
一 概述 1.整合目的 将所有对象的创建与管理任务交给Spring容器,降低程序的耦合度. 2.整合途径 将Spring容器注入到Web容器中. 3.具体实现 使用ServletContextList ...
- PHP之SimpleXML函数
使用php创建XML文件十分简单,使用SimpleXML那就更简便了,同时读取XML文件也十分方便.XML文件是直接在浏览器中打开,以自定义标签的方式直观简洁的方式展示给读者. 1.创建XML文件 h ...
- jquery操作select(选中,取值)
最近工作中总出现select 和 option问题,整理一下,内容大部分源于网络资料 一.基础取值问题 例如<select class="selector"></ ...
- 英文版win10更新以后, 中文软件变成乱码
原因是非Unicode程序的语言设置失效了 在区域设置里把当前系统区域设置改成"英语(美国)", 重启电脑后再改回"中文(简体, 中国)", 再重启就好了.
- 计算球面两点间距离实现Vincenty+Haversine
vincenty公式 精度很高能达到0.5毫米,但是很慢. Haversine公式半正矢公式,比vincenty快,精度没有vincenty高,也长使用. --------------------- ...
- RocketMQ读书笔记7——吞吐量优先的场景
[Broker端进行消息过滤] 在Broker端进行消息过滤,可以减少无效消息发送到Consumer,少占用网络宽带从而提高吞吐量. [过滤方式1——通过Tag过滤] [ 关于Tag和Key ] 对一 ...