Codility Tree Height
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的更多相关文章
- LeetCode Binary Tree Upside Down
原题链接在这里:https://leetcode.com/problems/binary-tree-upside-down/ Given a binary tree where all the rig ...
- 【Egret】中tree组件使用案例
Egret中tree组件使用案例,包含(文本过多时,自动换行功能) 下面代码结合http://bbs.egret.com/forum.php?mod=viewthread&tid=19028& ...
- BST 解析 (二)height and deletion
前面一章介绍了BST的结构和一些简单的基本功能,例如:insert,findMin,nextLarger等等.这一节主要讲解一些BST的delete node操作还有BST的height的分析以及一些 ...
- AVL Tree Insertion
Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...
- B+ Tree vs B Trees
原文地址:https://blog.csdn.net/dashuniuniu/article/details/51072795 引子 最近一直回顾自己曾经写的一些文档,有一篇是关于 Clang Rew ...
- 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 ...
- LeetCode Construct Binary Tree from String
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-string/description/ 题目: You need to ...
- 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 ...
- leetCode笔记--binary tree
993. Cousins in Binary Tree In a binary tree, the root node is at depth 0, and children of each dept ...
随机推荐
- Card Flip
卡牌效果:O(∩_∩)O,只做了webkit浏览器的效果,请用chrome~ 1.首先呢,先用一个框框把卡牌包住,然后呢,搞两个子元素作为卡牌正反面.当然咯,反面是看不见滴~ <section ...
- Python GUI 背景色与语法高亮主题配置
[补充] Python GUI 中 :ALT+P 可以重复上一条命令. ---------------------------------------------------------------- ...
- 微信H5页面内实现一键关注公众号
H5页面内实现关注公众号的微信JSSDK没有相关接口开放,因此就得动点脑筋来实现该功能了.下面的方法就是通过一种非常蹊跷的方式实现的. 首先,需要在公众号内发表一篇原创文章,注意是原创文章,然后由另一 ...
- PHP之初识PHP(1)
酸菜记 之 初识PHP 这是我从零基础了解PHP开始学习的笔记,共同学习,有不对之处,望指出共同学习. 一.初了解PHP PHP 是超文本预处理语言 php HyperText Preprocesso ...
- 分页sql优化
如果分页sql里包含排序: select * from (...order by id) where rownum <=20 因为要排序,所以即使是分页只取20条,执行计划还是要把所有满足条件的 ...
- 山东省第七届ACM省赛------Julyed
Julyed Time Limit: 2000MS Memory limit: 65536K 题目描述 Julyed is preparing for her CET-6. She has N wor ...
- 介绍MFSideMenu左右滑动控件的使用
昨天刚写完侧滑菜单的实例,今天在CocoaChina网站上看到一篇非常好的侧滑菜单设计案例文章,分享给大家.http://www.cocoachina.com/macdev/uiue/2013/071 ...
- 【Web】URI和URL,及URL的编码
URI和URL是什么,以及他们的区别 URL,Uniform Resource Locator,统一资源定位符.用于表示网络上服务器的资源所在位置,比如我们输入浏览器的地址. URI,Uniform ...
- 三层架构 与 MVC那点事儿
以下为转载内容: 地址 MVC与三层架构的异同点 首先先解释一下MVC. V即View.是视图的意思. C即Controler.是控制器的意思. M即Model,是模型的意思. 这三个里.最不容易理解 ...
- append追加的使用
#!/usr/bin/env python def fun(arg) : ret = [] for i in range(len(arg)) : if i % 2 ==1 : ret.append(a ...