CCI4.4/LintCode Balanced Binary Tree, Binary Tree, Binary Search Tree
Binary Tree: 0到2个子节点;
Binary Search Tree: 所有左边的子节点 < node自身 < 所有右边的子节点;
1. Full类型: 除最下面一层外, 每一层都有两个子节点;
2. Complete类型: 除最下面一层外为Full类型, 但是最下面一层最所有子节点靠左;
3. Balanced类型: 左右两个子树的长度相差小于等于一;
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
int checkHeight(TreeNode root){
if(root == null) return -; int leftHeight = checkHeight(root.left);
if(leftHeight == Integer.MIN_VALUE) return Integer.MIN_VALUE; int rightHeight = checkHeight(root.right);
if(rightHeight == Integer.MIN_VALUE) return Integer.MIN_VALUE; int heightDiff = leftHeight - rightHeight;
if(Math.abs(heightDiff) > ){
return Integer.MIN_VALUE;
}else{
return Math.max(leftHeight, rightHeight) + ;
}
}
public boolean isBalanced(TreeNode root) {
return checkHeight(root) != Integer.MIN_VALUE; // write your code here
}
}
traverse: 遍历; recursion: 递归(反复用自身); iteration: 迭代(循环);
3种遍历:
1. inorder: 左, node自身, 右(最左下角的node为第一个, 最右下角的node为最后一个);
2. preorder: node自身, 左, 右(node自身为第一个, 最右下角的node为最后一个);
3. postorder: 左, 右, node自身(最左下角的node为第一个, node自身为最后一个);
class Node {
int val;
Node left;
Node right;
public Node(int v) {
this.val = v;
}
}
public class BST {
public static void inorder(Node node) {
// 1. return condition
if(node == null) return;
// 2. process
inorder(node.left);
System.out.println(node.val);
inorder(node.right);
}
public static void preorder(Node node) {
if(node == null) return;
System.out.println(node.val);
preorder(node.left);
preorder(node.right);
}
public static void postorder(Node node) {
if(node == null) return;
postorder(node.left);
postorder(node.right);
System.out.println(node.val);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Node root = buildTree();
inorder(root);
}
public static Node buildTree() {
Node ten = new Node();
Node one = new Node();
Node fifteen = new Node();
Node five = new Node();
five.left = one;
ten.left = five;
ten.right = fifteen;
return ten;
}
}
CCI4.4/LintCode Balanced Binary Tree, Binary Tree, Binary Search Tree的更多相关文章
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- Lintcode: Remove Node in Binary Search Tree
iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. ...
- Lintcode: Search Range in Binary Search Tree
Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...
- LintCode题解之Search Range in Binary Search Tree
1.题目描述 2.问题分析 首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字. 3.代码 /** * Definition of TreeNode ...
- Lintcode: Insert Node in a Binary Search Tree
Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...
- 【Lintcode】087.Remove Node in Binary Search Tree
题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given v ...
- 【Lintcode】011.Search Range in Binary Search Tree
题目: Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find a ...
- 【Lintcode】095.Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
随机推荐
- PhoneGap初试!
最近公司准备开发一个移动应用,方便起见准备开发web项目,用PhoneGap打包成iOS与Android平台的应用.对PhoneGap完全不了解,所以先装个试下.折腾了大半天,总算弄出点儿眉目,整理下 ...
- Monkey for iOS(CrashMonkey4IOS)
CrashMonkey4IOS介绍 支持真机测试.模拟器测试 支持收集系统日志(Systemlog).崩溃日志(Crashlog).instrument行为日志 支持测试报告截图,绘制行为轨迹 支持测 ...
- Websocket 协议解析
WebSocket protocol 是HTML5一种新的协议.它是实现了浏览器与服务器全双工通信(full-duplex). 现 很多网站为了实现即时通讯,所用的技术都是轮询(po ...
- Cheap Hollister Clothing
(link to hollisterco site), Spectacles don't simply take care of the eye area inside sun; Putting th ...
- 手把手教你在Ubuntu上安装Apache、MySql和PHP
1:首先安装apache:打开终端(ctrl+Alt+t), 输入命令:sudo apt-get install apache2即可安装, 安装完后,打开浏览器,在地址栏输入:localhost或者h ...
- Apache MiNa 实现多人聊天室
Apache MiNa 实现多人聊天室 开发环境: System:Windows JavaSDK:1.6 IDE:eclipse.MyEclipse 6.6 开发依赖库: Jdk1.4+.mina-c ...
- 原生js实现放大镜效果
今天做任务的时候,有一个任务就是让实现电商网站常用的放大镜效果,类似于这样的效果,之前并没有做过这种放大镜效果,刚开始的思路是对图片进行裁剪,但是后来发现实在是难以实现,于是求助了万能的谷歌,发现一个 ...
- 例子:Background Audio Streamer Sample
The Background Audio Streamer sample demonstrates how to create an app that uses a MediaStreamSource ...
- css中的1px并不总等于设备的1px(高分辨率不等 低分辨等)
在css中我们一般使用px作为单位,在桌面浏览器中css的1个像素往往都是对应着电脑屏幕的1个物理像素,这可能会造成我们的一个错觉,那就是css中的像素就是设备的物理像素.但实际情况却并非如此,css ...
- 一个Java递归删除目录的方法
public static void delDir(File f) { // 判断是否是一个目录, 不是的话跳过, 直接删除; 如果是一个目录, 先将其内容清空. if(f.isDirectory() ...