寻找一棵树的深度?

编程入门 行业动态 更新时间:2024-10-13 14:25:27
本文介绍了寻找一棵树的深度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对二叉树和递归非常陌生。我的程序是找到树的高度,但是我对我的程序为什么不起作用感到困惑。

I am very new to binary tree and recursion. My program is to find the height of the tree but I am a bit confused as to why my program doesn't work.

struct Node { int value; Node *left; Node *right; } int heightOfTree(Node node){ if(node ==NULL) { return 0; } else { int lheight=heightOfTree(node->left); int rheight = heightOfTree(node->right); if(lheight>rheight) { return lheight; } else { return rheight; } } }

我在网上跟踪了伪代码,因此我自己实现了它,因为我不想只复制粘贴。我试图插入很多节点,但是当我运行程序时,我总是得到0高度吗?谢谢

I followed a pseudocode online so I implemented it myself because I don't want to just copy and paste. I tried to insert a lot of nodes but When I run my program I always get 0 height? Thank you

推荐答案

return lheight + 1;

return rheight + 1;

您需要在每个级别上增加高度。

You need to increment the height at each level.

更多推荐

寻找一棵树的深度?

本文发布于:2023-11-29 21:06:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647629.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:一棵树   深度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!