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)便是使用二叉树实现 ...
随机推荐
- css 之 文本缩进属性(text-indent)
文章转自:http://www.10wy.net/Article/CSS/CSS_list_8.html查看更多更专业性的文章请到:网页设计网 文本缩进属性(text-indent) 这个属性设定文本 ...
- 使用JavaScript实现复选框全选与取消的功能
实现效果: html代码: <body> <input type="checkbox" id="checkAll"/>全选<br& ...
- 关于JQuery简单介绍
jQuery是一个兼容多浏览器的javascript库,核心理念是写得更少,做得更多.如今,jQuery已经成为最流行的javascript库,在世界前10000个访问最多的网站中,有超过55%在使用 ...
- JS总结 节点
nodeName 获取节点名称 元素节点:返回标记名称 属性节点:返回属性名称 文本节点:返回文本#text nodeTyle 获取节点类型 元素节点:返回1 属性节点:返回2 文本节点:返回3 n ...
- Linux系统性能分析
http://c.biancheng.net/cpp/html/2782.htmlLinux系统性能分析 这篇教程的目的是向大家介绍一些免费的系统性能分析工具(命令),使用这些工具可以监控系统资源使用 ...
- 【其他】win7创建wifi热点共享给手机使用
出门在外,有时候网络有诸多不便,需要用笔记本创建wifi热点给手机用:本人测试xp怎么配置都不好使,但win7有可行的方案,不依赖第三方软件. 详述如下: 场景一:win7 + A(PC机)(用无线连 ...
- 将时区格式的时间转换为易于阅读的标准格式"yyyy-MM-dd"
Date的显示格式就是时区格式, String 标准格式 = new SimpleDateFormat("yyyy--MM--dd").format(new Date());
- 关于Python中的文件操作(转)
总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Pyth ...
- ARM9的中断控制器
简要复习一下ARM9中断控制器的控制过程: 1.首先能识别触发的中断(对应中断源必须打开,然后查询当前中断状态寄存器),硬件会操控PC跳到中断向量入口(IRQ_HANDLE,硬件控制的只要是IRQ中断 ...
- Cosh.2
没壳 直接拖 一开始 字符长度不对. 所以 输入都超过5 就没什么问题了 发现下面的跳转都是跳到eorr的 那不跳的条件呢 看来他们必须要相等.单步跟踪就会发现eax指向的是我们的Seri ...