数据结构与算法-第12章二叉树和其他树-003求二叉树的高度

本文提供了一段使用后序遍历方法来计算二叉树高度的Java代码实现。该算法通过递归方式寻找左右子树的高度,并返回较高子树高度加一作为当前节点的高度。

The code to find the tree height using a postorder traversal is given below.

 1 public class BinaryTreeHeight
 2 {
 3    /** @return tree height */
 4    public static int height(BinaryTreeNode t)
 5    {
 6       if (t != null)
 7       {// nonempty tree
 8          // find height of left subtree
 9          int hLeft = height(t.leftChild);
10 
11          // find height of right subtree
12          int hRight = height(t.rightChild);
13 
14          // return overall height
15          return Math.max(hLeft, hRight) + 1;
16       }
17       else
18          return 0;
19    }
20 }

 

转载于:https://www.cnblogs.com/shamgod/p/5295580.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值