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. JavaScript笔记 Function

    在JavaScript中方法由两部分组成: 方法名和方法体. JavaScript中的方法跟其他传统面向对象语言不同,它跟普通的变量没有区别,唯一不同点是它是Function对象,因此它会有一些Fun ...

  2. 使用django rest framework

    django 刚接触,想做一些restful api , google了一下,发现有现成的框架.Django REST framework. 对使用做下记录: 安装 从http://django-re ...

  3. fetch 关于七牛的content-type 的问题

    七牛要的Content-Type: multipart/form-data; boundary=<frontier>:那个boundary(formdata分隔符)是浏览器自己加的,不用管 ...

  4. python处理xml文件

    参考:https://docs.python.org/2/library/xml.etree.elementtree.html 例子: <?xml version="1.0" ...

  5. tensorflow 学习笔记

    tensorflow一些函数: 1.tf.ones(shape,type=tf.float32,name=None)      tf.ones([2, 3], int32) ==> [[1, 1 ...

  6. exp_tools

    #pwntools # github https://github.com/Gallopsled/pwntools 在线帮助文档 https://docs.pwntools.com/en/stable ...

  7. Jersey 2 + Maven + Tomcat + IntelliJ IDEA 搭建RESTful服务

    本文参考以下内容: [1] Starting out with Jersey & Apache Tomcat using IntelliJ [2] [Jersey]IntelliJ IDEA ...

  8. IIS出现Service Unavailable 错误

    IIS访问操作出现以下问题时要如何解决:

  9. javascript高级程序设计第5章,引用类型

    object类型: 创建object实列的方式有两种,一种是new()方法,一种是对象字面量表示法: 第一种法方:  var obj = new object(); obj.name = 'name' ...

  10. stm8的IIC库的使用

    一.前言 stm8是一款低功耗的MCU芯片,它具备stm32库函数和资源丰富的优势.也同时具有价格便宜,低功耗的特点.在一些项目中,能起到很好的作用.下面我介绍一下stm8的IIC硬件库函数驱动代码及 ...