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. opencv人脸识别提取手机相册内人物充当数据集,身份识别学习(草稿)

    未写完 采用C++,opencv+opencv contrib 4.1.0 对手机相册内人物opencv人脸识别,身份识别学习 最近事情多,介绍就先不介绍了 photocut.c #include & ...

  2. 设计模式课程 设计模式精讲 3-4 依赖倒置原则讲解+coding

    1 课程讲解 1.1 定义 1.2 优点 1.3 细节描述 2 代码演练 2.0 代码展示优点 2.1 非面向接口编程 2.2 面向接口编程1 传参 2.3 面向接口编程2 构造函数 2.4 面向接口 ...

  3. ThinkPHP6源码:从Http类的实例化看依赖注入是如何实现的

    ThinkPHP 6 从原先的 App 类中分离出 Http 类,负责应用的初始化和调度等功能,而 App 类则专注于容器的管理,符合单一职责原则. 以下源码分析,我们可以从 App,Http 类的实 ...

  4. 信息论相关概念:熵 交叉熵 KL散度 JS散度

    目录 机器学习基础--信息论相关概念总结以及理解 1. 信息量(熵) 2. KL散度 3. 交叉熵 4. JS散度 机器学习基础--信息论相关概念总结以及理解 摘要: 熵(entropy).KL 散度 ...

  5. 02.当构造参数过多时使用builder模式

    前言 <Effective Java>中文第三版,是一本关于Java基础的书,这本书不止一次有人推荐我看.其中包括我很喜欢的博客园博主五月的仓颉,他曾在自己的博文<给Java程序猿们 ...

  6. 使用外网访问Flask项目

    在学习flask过程中,想使用手机访问项目,根据flask手册中可以将 app.run(host='192.168.1.109', port=8000,debug=True) 但是发现手机依然无法连接 ...

  7. Python 文件和目录操作学习

    文件与文件路径 文件有两个关键属性:文件名和路径. 路径指明了文件在计算机上的位置. 文件名中,最后一个句点之后的部分称为文件的"扩展名",它指出了文件的类型 目录也叫文件夹,文件 ...

  8. selenium webdriver 定位元素 第一部分

    static final WebDriver driver = ExplorerBase.IESetting(); // 实例化一个浏览器对象 @Test //@Ignore public void ...

  9. 笔记-python-lib—data types-enum

    笔记-python-lib—data types-enum 1.      enum Source code: Lib/enum.py 文档:https://docs.python.org/3/lib ...

  10. broadcom sdk command

    1.查看端口link状态 BCM.0>ps 2.查看vlan BCM.0>vlan show 3.查看pvlan BCM.0>pvlan show 4.CPU发包 BCM.0> ...