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的更多相关文章

  1. LintCode Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  2. 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. ...

  3. 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 ...

  4. LintCode题解之Search Range in Binary Search Tree

    1.题目描述 2.问题分析 首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字. 3.代码 /** * Definition of TreeNode ...

  5. 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 ...

  6. 【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 ...

  7. 【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 ...

  8. 【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 ...

  9. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

随机推荐

  1. <java基础学习>02JAVA的基础组成(2)

    60000-0000 0000-0000 0000-0000 0000-0110 0000-0110 -6这个数的正数的二进制取反,再加1 0000-0110取反: 1111-1001 + 0000- ...

  2. 深入了解 JavaScript 中的 for 循环

    在ECMAScript5(简称 ES5)中,有三种 for 循环,分别是: 简单for循环 for-in forEach 在2015年6月份发布的ECMAScript6(简称 ES6)中,新增了一种循 ...

  3. Nginx配置单主机多域名

    http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; ...

  4. gif显示

    public void gifplay(string path,ref Panel panel) { try{ Bitmap animatedGif = new Bitmap(path ); Grap ...

  5. Common.Logging log4net Common.Logging.Log4Net 配置

    1.log4net 单独配置 log4net支持多种格式的日志输出,我这里只配置输出到本地的txt文件这种格式. <log4net> <root> <appender-r ...

  6. EasyUI之加载js文件顺序

    在使用EasyUI时,需要引用EasyUI框架的js文件,要注意加载的顺序,jquery.min.js一定要第一个被加载.

  7. 八皇后java算法

    import java.util.Date; public class EightQueen { public static void main(String[] args) {  long star ...

  8. Git忽略规则及.gitignore规则不生效的解决办法

    在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件).这个文件每一行保存了一个匹配的规则例如: # 此为注 ...

  9. js对象的深度克隆

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. python获取字母在字母表对应位置的几种方法及性能对比较

    python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...