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. everthing 添加右键菜单

    Tool --> Options --> General -->勾上 Show folder context menus

  2. 计算机网络(9)-----TCP可靠传输的实现

    TCP可靠传输的实现 以字节为单位的滑动窗口 滑动窗口的滑动是以字节为单位的,发送方A和接收方B在TCP三次握手的前两次握手时协商好了发送窗口和接受窗口的大小,发送方A根据B发送来的确认连接报文中标明 ...

  3. 计算机网络(8)-----TCP报文段的首部格式

    TCP报文段的首部格式 概述 TCP报文段首部的前20个字节是固定的,因此TCP首部的最小长度是20字节. 源端口和目标端口 各占2个字节,分别写入源端口号和目的端口号. 序列号 占4个字节,表示本报 ...

  4. spark-sql性能测试

    一,测试环境       1) 硬件环境完全相同:              包括:cpu/内存/网络/磁盘Io/机器数量等       2)软件环境:              相同数据       ...

  5. javaSE学习路线

    Java SE大致可分为以下几块内容: n  对象导论:如何用面向对象的思路来开发 n  深入JVM:Java运行机制以及JVM原理 n  面向对象的特征:封装.继承.抽象.多态 n  数组和容器:容 ...

  6. HTML5 拖动

    触发的事件有:dragstart事件.drag事件和dragend事件. 按下鼠标键并开始移动鼠标的时候,会在被拖拽的元素上触发dragstart事件.这时候光标变成”不能放”符号(圆环中有一条反斜线 ...

  7. svn co

    svn  co  的用法经常有两种:    第一种:  直接  svn  co    http://svnserver/mypro/trunk                 此时, 会在你的当前目录 ...

  8. 涵涵和爸爸习惯养成进度表(二)(May 30 - )

    规则说明 22天内,没有哭脸,不超过三个无表情脸,可以给一个奖励(动画书等) 涵涵违反规则,在爸爸和妈妈都同意的情况下,可以给无表情脸 爸爸违反规则,在妈妈和涵涵都同意的情况下,可以给无表情脸 获奖记 ...

  9. Spring学习笔记(1)——资源加载

    <!-- 占坑,迟点补充底层原理 --> Spring支持4种资源的地址前缀 (1)从类路径中加载资源——classpath: classpath:和classpath:/是等价的,都是相 ...

  10. ant打包webservice jar

    <project name="helloworldservice" basedir="." default="deploy"> ...