Binary Tree
Definition: at most two children node.

Binary Tree Example:

                10 ==root

               /      \

              13          15  cur

             /   \     /  \

            21  72      12    2

            /  \

          null   null

class TreeNode{

  int value;

  TreeNode * left;

  TreeNode * right;

  TreeNode * parent //point to this node's parent node.

}
面试常见题型:

基本知识点1: tree traverse

1. pre-order.

2.in-order.

3.post-order.

关键点:base case通常就是叶子节点下面的空节点。

Balanced binary tree:

对于树的任意一个节点,左子树和右子树的高度差不超过1

Complete binary tree(完全二叉树)

底层是一个数组,数组内部的数字必须是连续的,不能有空余的内存空间。

Binary Search Tree(二叉查找树)

          10

         /      \

        5        15

       /   \       /     \

      2     7      12     20

注意:对于根节点10,必须整个左子树(左子树上的所有节点)都必须比10小,整个右子树(右子树上的所有节点)必须比10大。

同时binary search tree不允许有重复的node;

Binary tree 往往是最常见的和recursion结合最紧密的面试题目类型。

理由:

1.每层的node所具备的性质,传递的值和下一层的性质往往一致,比较容易定义recursive rule。

2.base case: null pointer under the leaf node.

3.Example1:int getHeight(Node root)

4.Example2:统计tree里面有多少个node。

常见面试题型:

How to get integer value(height) for a problem with size = n? But how?

GetHeight of a binary tree?

public int getHeight(TreeNode root){

  if(root == null){

  return 0;

}  

  int left = getHeight(root.left);

  int right = getHeight(root.right);

  return 1 + Math.max(left,right);

}

Time = O(n);

space = O(n) == O(height);

---------------------------------------------------------------------------------------------------------------------------

Q3:How to determine whether a binary tree is a balanced binary tree?

public boolean isBalanced(TreeNode tree){

    if(root == null){

      return true;

}  

    int left = getHeight(root.left);

    int right = getHeight(root.right);

    if(Math.abs(left - right) > 1){

      return false;

  }  

     return  isBalanced(root.left) && isBalanced(root.right);

}

时间复杂度分析:

            isBalanced(n/2 + n/2)

             /      \

          getHeight  getHeight

          (n/4 + n/4)  (n/4 + n/4)

因为是一个平衡二叉树所以:层数logn   So: Time : O(nlogn)

---------------------------------------------------------------------------------------------------------------------------

Q4:怎么判断一颗二叉树左右两边是不是对称的?

          10

         5a  |   5b

       1a   3a  |    3b    1b

    2a4a 6a8a   |    8b6b  4b2b

 public boolean isSymmetric(TreeNode noe,TreeNode two){

    if(one == null && two == null){

      return true;

    }
    if(one ==null || two == null){

      return false;

}

    if(one.value == two.value){

      return false;

}

    return isSymmetric(one.left,two.right) && isSymmetric(one.right,one.left);

}

Time = O(n);

space = O(n) -> O(height)   if the tree is balaced -> O(logn)

---------------------------------------------------------------------------------------------------------------------------

Q5:

 

    

Binary Tree和Binary Search Tree的更多相关文章

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

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

  2. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  4. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  5. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  6. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  7. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  9. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  10. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

随机推荐

  1. Update(stage3):第1节 redis组件:4、安装(略);5、数据类型(略);6、javaAPI操作;

    第三步:redis的javaAPI操作 操作string类型数据 操作hash列表类型数据 操作list类型数据 操作set类型的数据 详见代码

  2. iOS 开发中常用的排序(冒泡、选择、快速、插入、希尔、归并、基数)算法

    1.冒泡排序: 冒泡算法是一种基础的排序算法,这种算法会重复的比较数组中相邻的两个元素.如果一个元素比另一个元素大(小),那么就交换这两个元素的位置.重复这一比较直至最后一个元素.这一比较会重复n-1 ...

  3. 字符串操作函数:JSON.parse()与JSON.stringify()的区别,字符串转数组 str.split(','),数组转字符串String(),以及对象拼接合并Object.assign(),数组拼接合并concat()

    1.JSON.parse()  把字符串转化为 json 对象 例如 arr={ , "site":"www.runoob.com" } var obj = J ...

  4. [原]Java工程打包注意事项

    注意事项(持续增加...): 如果Java工程中用到了注解,在用eclipse打jar包时需要注意一下,勾上“Add directory entries”,否则注解的类会注册不上

  5. python 顺序执行任务

    #!/usr/bin/python import os import time start_command="sh start-etl.sh " es_mac_confPath = ...

  6. 【摘录自MDN】预定义函数

    JavaScript语言有好些个顶级的内建函数: eval() eval()方法会对一串字符串形式的JavaScript代码字符求值. uneval()  uneval()方法创建的一个Object的 ...

  7. SpringBoot下配置Druid

    什么是Druid:Druid是阿里发开的一套基于database的监控平台,相对于其他监控来说对于中文的支持更亲民.. 前言:最近这段时间发现项目整体运行响应速度较慢,打算对系统进行深层次的优化(尤其 ...

  8. Python 基础之面向对象之八步理解装饰器

    装饰器:在不改变原有代码的情况下,为该原函数扩展新功能特征:返回新函数,替换旧函数语法:@ 语法糖 1.装饰器原型 #例1: def kuozhan(func):    def newfunc():  ...

  9. 开启Linux服务器vnc远程服务详细步骤

    1.安装rpm包 ,如果没有这个包,请提前下载好,然后输入命令安装   yum localinstall  /usr/local/tigervnc-server-1.1.0-24.el6.x86_64 ...

  10. 匈牙利命名法、Camel命名法与Pascal命名法

    Camel命名法:即骆驼式命名法,首字母小写,采用该命名法的名称看起来就像骆驼的驼峰一样高低起伏.Camel命名法有两种形式: 1.混合使用大小写字母,例如runFast 2.单词之间加下划线,例如r ...