Binary Tree和Binary Search Tree
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的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [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 ...
随机推荐
- Update(Stage4):sparksql:第3节 Dataset (DataFrame) 的基础操作 & 第4节 SparkSQL_聚合操作_连接操作
8. Dataset (DataFrame) 的基础操作 8.1. 有类型操作 8.2. 无类型转换 8.5. Column 对象 9. 缺失值处理 10. 聚合 11. 连接 8. Dataset ...
- mysql update 修改多个字段and的语法问题
在MySQL里面update一条记录,语法都正确的,但记录并没有被更新... 问题语句 执行之前的记录是这样的: 执行之后的记录是这样的: 可以看到,实际上是有效果的: why? 看起来,语法是完全没 ...
- mcast_get_if函数
#include <errno.h> int sockfd_to_family(int); int mcast_get_if(int sockfd) { switch (sockfd_to ...
- springboot默认创建的bean是单实例
默认是单例 通过注解@Scope("prototype"),将其设置为多例模式 参考: 曾经面试的时候有面试官问我spring的controller是单例还是多例,结果我傻逼的回答 ...
- Python - 编程技巧,语法糖,黑魔法,pythonic
参考,搬运 http://python-web-guide.readthedocs.io/zh/latest/idiom/idiom.html 待定 1. Python支持链式比较 # bad a = ...
- 什么是Socket:
先了解一些前提: 网络由下往上分为 物理层 .数据链路层 . 网络层 . 传输层 . 会话层 . 表现层 和 应用层.通过初步了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对 ...
- Python环境搭建-1 Python介绍
Python翻译成汉语是蟒蛇的意思,并且Python的logo也是两条缠绕在一起的蟒蛇的样子,然而Python语言和蟒蛇实际上并没有一毛钱关系. Python语言是由荷兰程序员Guido van Ro ...
- 「TJOI2013」最长上升子序列
「TJOI2013」最长上升子序列 传送门 这个 \(\text{DP}\) 应该都会撒: \[dp_i = \max_{j < i,a_j < a_i}\left\{dp_j\right ...
- 「AHOI2008」紧急集合/聚会
题目描述 这次也是很长的题面啊\(qwq\) 题目大意如下: 给定一棵\(N\)个节点的树以及\(M\)次询问,每次询问给出\(x,\ y,\ z\)三个节点,程序需要在树上找一个点\(p\) 使得\ ...
- Py西游攻关之基础数据类型(三)-元组
Py西游攻关之基础数据类型 - Yuan先生 https://www.cnblogs.com/yuanchenqi/articles/5782764.html 六 tuple(元组) 元组被称为只读列 ...