public class HeightOfTreeSolution {
static int height=-1;
public int solution(Tree T) {
// write your code in Java SE 8
if (T == null) return height;
height = heightOfTree(T);
return height;
} public int heightOfTree(Tree node){
if (node!=null) {
if (node.l==null) {
return 1+heightOfTree(node.r);
}
if (node.r==null) {
return 1+heightOfTree(node.l);
}
else{
return 1+Math.max(heightOfTree(node.r),heightOfTree(node.l));
}
}
return 0;
}
}

问题描述:

Write a function:

class Solution { public int solution(Tree T); }

that, given a non-empty binary tree T consisting of N nodes, returns its height. For example, given tree T shown in the figure above, the function should return 2, as explained above. Note that the values contained in the nodes are not relevant in this task.

Codility Tree Height的更多相关文章

  1. LeetCode Binary Tree Upside Down

    原题链接在这里:https://leetcode.com/problems/binary-tree-upside-down/ Given a binary tree where all the rig ...

  2. 【Egret】中tree组件使用案例

    Egret中tree组件使用案例,包含(文本过多时,自动换行功能) 下面代码结合http://bbs.egret.com/forum.php?mod=viewthread&tid=19028& ...

  3. BST 解析 (二)height and deletion

    前面一章介绍了BST的结构和一些简单的基本功能,例如:insert,findMin,nextLarger等等.这一节主要讲解一些BST的delete node操作还有BST的height的分析以及一些 ...

  4. AVL Tree Insertion

    Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...

  5. B+ Tree vs B Trees

    原文地址:https://blog.csdn.net/dashuniuniu/article/details/51072795 引子 最近一直回顾自己曾经写的一些文档,有一篇是关于 Clang Rew ...

  6. LeetCode 655. Print Binary Tree (C++)

    题目: Print a binary tree in an m*n 2D string array following these rules: The row number m should be ...

  7. LeetCode Construct Binary Tree from String

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-string/description/ 题目: You need to ...

  8. Red–black tree ---reference wiki

    source address:http://en.wikipedia.org/wiki/Red%E2%80%93black_tree A red–black tree is a type of sel ...

  9. leetCode笔记--binary tree

    993. Cousins in Binary Tree In a binary tree, the root node is at depth 0, and children of each dept ...

随机推荐

  1. PHP文件操作:遍历文件目录

    <?php /*遍历目录,列出目录中的文件 * array scandir(string $directory [,int $sorting_order]) * $directory为待遍历目录 ...

  2. mybatis,sql 批量更新

    <update id="update81OrderStatus" parameterType="java.util.Map">    update ...

  3. OpenGL FAQ

    转自:http://www.cnblogs.com/indif/archive/2011/04/22/2024659.html 1.什么是OpenGL?OpenGL即开放图形库(Open Graphi ...

  4. [转]CocoaPods安装和使用教程

    转载地址:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装CocoaPods? 如何使用Coco ...

  5. GCD一些常用的方法

        //1.创建主线程(串行)     dispatch_async(dispatch_get_main_queue(), ^{         //刷新界面代码     });     //2. ...

  6. 代理 XP”组件已作为此服务器安全配置的一部分被关闭。系统管理员可以使用 sp_configure 来启用“代理 XP”。

    新建维护计划的时候遇到下图的报错信息 标题: Microsoft SQL Server Management Studio------------------------------ “代理 XP”组 ...

  7. (转)解决Mac OS X上PhpStorm不能输入中文

    看到Netbeans上类似问题的解决办法: /Applications/netbeans/NetBeans 6.7.1/Content/Resource/netbeans/etc/netbeans.c ...

  8. vue.js存储--localStorage

    //list例子:绑定从localStorage中读取的数据,动态添加list并监听将数据变化存储在localStorage中,绑定点击事件改变样式, 页面 data数据: input_name:'' ...

  9. 。i节点

    http://blog.csdn.net/feiyinzilgd/article/details/5609157  linux中,文件查找不是通过文件名称来查找的.实际上是通过i节点来实现文件的查找定 ...

  10. 更改SQL Server 数据库的排序规则

    更改数据库的排序规则,SQL提示 5030 的错误,错误信息如下: The database could not be exclusively locked to perform the operat ...