求二叉树的次深度

vssvss 2011-07-06 10:15:53
问题描述:对于一个二叉树,有很多叶子结点,现在需要一个函数求所以叶子节点深度第二大的。

--------------------------------- 1
---------------------------------/ \
--------------------------------2---3
-------------------------------/ \ / \
------------------------------4 5 6 7
-----------------------------/ /
----------------------------8 9
---------------------------/ \
--------------------------10 11
这个树的最大深度是5(节点10,11),次深度为4(节点9)。
现在求一个算法来获取这颗树的次深度。语言不限
坐等高手解惑!
...全文
189 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
dayuly55 2011-07-06
  • 打赏
  • 举报
回复
请问这个深度为5,次深度为3是怎吗算出来的啊!菜鸟请教!!!![Quote=引用 3 楼 felixz2010 的回复:]
引用 1 楼 dizuo 的回复:

递归求二叉树深度:
一、如果二叉树为空,则深度为0;
二、否则二叉树的深度为左右子二叉树深度的较大值加1。

深度减1不就是次深度。。。


但会出现如下情况
当树为
--------------------------------- 1
---------------------------------/ \
-----------……
[/Quote]
felixz2010 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 liuzhengxi2010 的回复:]

先将各个叶子节点的深度求出再找次深度这样行不?
[/Quote]

有没有可能直接在求深度的递归算法上进行修改实现呢?

以下是求深度的递归算法
==============binary tree deep====================
public static int Deep(BinaryTree tree)
{
if (tree.LeftTree == null && tree.RightTree == null)
return 1;

int l = 0, r = 0;
if (tree.LeftTree != null) l = Deep(tree.LeftTree);
if (tree.RightTree != null) r = Deep(tree.RightTree);

return l > r ? l + 1 : r + 1;
}
liuzhengxi2010 2011-07-06
  • 打赏
  • 举报
回复
先将各个叶子节点的深度求出再找次深度这样行不?
冰山上的闪电 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 felixz2010 的回复:]

引用 1 楼 dizuo 的回复:

递归求二叉树深度:
一、如果二叉树为空,则深度为0;
二、否则二叉树的深度为左右子二叉树深度的较大值加1。

深度减1不就是次深度。。。


但会出现如下情况
当树为
--------------------------------- 1
---------------------------------/ \
----------……
[/Quote]
原来次深度是这个意思,看来理解错了。。。
felixz2010 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dizuo 的回复:]

递归求二叉树深度:
一、如果二叉树为空,则深度为0;
二、否则二叉树的深度为左右子二叉树深度的较大值加1。

深度减1不就是次深度。。。
[/Quote]

但会出现如下情况
当树为
--------------------------------- 1
---------------------------------/ \
--------------------------------2---3
-------------------------------/ \ / \
------------------------------4 5 6 7
-----------------------------/
----------------------------8
---------------------------/
--------------------------10
此时,深度为5, 次深度为3,而不是5-1;
冰山上的闪电 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dizuo 的回复:]

递归求二叉树深度:
一、如果二叉树为空,则深度为0;
二、否则二叉树的深度为左右子二叉树深度的较大值加1。

深度减1不就是次深度。。。
[/Quote]
+1
ryfdizuo 2011-07-06
  • 打赏
  • 举报
回复
递归求二叉树深度:
一、如果二叉树为空,则深度为0;
二、否则二叉树的深度为左右子二叉树深度的较大值加1。

深度减1不就是次深度。。。
vssvss 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 qq675927952 的回复:]
C/C++ code


void secondDepth(node* root,int depth,int& secondDepth, int maxDepth)
{
if(root->lchild==null && root->rchild==null)
{
depth ++;
if(depth > maxDepth)
……
[/Quote]

void secondDepth(node* root,int depth,int& secondDepth, int maxDepth)
{
if(root->lchild==null && root->rchild==null)
{
depth ++;
if(depth > maxDepth)
{
secondDepth = maxDepth ;
maxDepth = depth;
} else if(depth > secondDepth && depth!=maxDepth)
{
secondDepth = depth;
}
return ;
}
if(root->lchild)
secondDepth( root->lchild,depth+1,secondDepth,maxDepth);
if(root->rchild)
secondDepth( root->rchild,depth+1,secondDepth,maxDepth);
}




这样才对了 谢谢大家的踊跃参与 来者都有分
felix的也是对的 呵呵 结贴给分咯
felixz2010 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 qq675927952 的回复:]

C/C++ code

void secondDepth(node* root,int depth,int& secondDepth, int maxDepth)
{
if(root->lchild==null && root->rchild==null)
{
depth ++;
if(depth > maxDepth)
{
……
[/Quote]

public void SecondDepth(BinaryTree root, int depth, ref int secondDepth,ref int maxDepth)
{
if (root.LeftTree == null && root.RightTree == null)
{

if (depth+1 > maxDepth)
{
secondDepth = maxDepth;
maxDepth = depth+1;
}
else if (depth+1 > secondDepth && depth+1 != maxDepth)
{
secondDepth = depth+1;
}
return;
}
if (root.LeftTree != null)
SecondDepth(root.LeftTree, depth + 1, ref secondDepth,ref maxDepth);
if (root.RightTree != null)
SecondDepth(root.RightTree, depth + 1, ref secondDepth,ref maxDepth);
}
felixz2010 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 qq675927952 的回复:]

C/C++ code

void secondDepth(node* root,int depth,int& secondDepth, int maxDepth)
{
if(root->lchild==null && root->rchild==null)
{
depth ++;
if(depth > maxDepth)
{
……
[/Quote]

你好,当使用如下树做测试时返回为0,参数depth,secondDepth,maxDepth初始均设为0
--------------------------------- 6
---------------------------------/ \
--------------------------------3---7
-------------------------------/ \
------------------------------2 5
-----------------------------/ /
----------------------------1 4

期望为: 次深度:2
实际为:0

felixz2010 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dayuly55 的回复:]

请问这个深度为5,次深度为3是怎吗算出来的啊!菜鸟请教!!!!引用 3 楼 felixz2010 的回复:
引用 1 楼 dizuo 的回复:

递归求二叉树深度:
一、如果二叉树为空,则深度为0;
二、否则二叉树的深度为左右子二叉树深度的较大值加1。

深度减1不就是次深度。。。


但会出现如下情况
当树为
------------------------------……
[/Quote]

次深度是指:出现叶子节点(左右子树均为NULL)的层数,且其层数小于树深度(当不存在时返回0)
qq675927952 2011-07-06
  • 打赏
  • 举报
回复

void secondDepth(node* root,int depth,int& secondDepth, int maxDepth)
{
if(root->lchild==null && root->rchild==null)
{
depth ++;
if(depth > maxDepth)
{
secondDepth = maxDepth ;
maxDepth = depth;
} else if(depth > secondDepth)
{
secondDepth = depth;
}
return ;
}
if(root->lchild)
secondDepth( root->lchild,depth+1,secondDepth,maxDepth);
if(root->rchild)
secondDepth( root->rchild,depth+1,secondDepth,maxDepth);
}
qq675927952 2011-07-06
  • 打赏
  • 举报
回复
可以用回溯法,求根结点到叶子结的的长度,取次大的就行了。
yaoweijq 2011-07-06
  • 打赏
  • 举报
回复
我觉得这个次深度的定义应当明确
比如说所有的结点要么没有孩子
要么有两个孩子
这种情况下次深度如何整?
或者满二叉树的次深度怎么定义
以二叉树的递归定义来说
这个求次深度也可以递归求

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧