Tree总结
树结构问题因为容易写出解法,因此经常出现在面试题中
1. 树的种类
1) Tree
2) Binary Trees
3) Binary Search Trees(BST) : used to sorted or ordered data. //解决方案:recursion
查找操作(fast and simple):O(logn)
插入和删除:O(logn)
打印:O(n)
4) Heap: find the maximum value / minimum value(min-heap) in constant time.
插入和删除:O(logn)
查找:O(n) ,因为除了顶点,左右子书是无序的
2. 通常的查询方法
注意:如果查找的树结构是无序的,时间复杂度是O(n),所以复杂的树要避免使用普通的树结构
1)BFS :空间开销大
2) DFS
3. Traversals
当需要遍历树中的每个点时使用的方法,遍历方法有很多。 // recursive
最寻常的Depth-first-traversals for binary tree的种类
1) Pre-order : a node is always visited before any its children, then left first
2) In-order : The left subtree is visited first, then the node itself, and then the nodes's right subtree.
3) Post-order: left, right, node. A node is always visited after all its children.
树类型的经典问题:
1. 求树高
思路:在遇到树问题时,先考虑是否能使用recursive的方法解决。本题中,我们可以看出,任意子节点的树高是它两个子节点树高的最大值+1
public static int treeHeight( Node n){
if(n == null) return 0;
return 1 + Math.max( treeHeight(n.left), treeHeight(n.right)
);
}
2. Pre-order traversal
思路 1 :pre-order traversal 要求先打印root node,然后是左子树,再右子树。注意:左子树是直接遍历到底的。从recursive的观点来看,这个问题可以分成三个部分
1) 打印root node
2) 左子树
3) 右子树
public static void preOrderT(Node n) {
if(n == null) return;
System.out.println(n.value); //意思意思
preOrderT(n.left);
preOrderT(n.right);
}
inorder和postorder同理
思路 2 (非 recursion):recursive的方法在原理上与栈类似,因此本能应选择栈作为替代的数据结构
public static void preOrderTStack(Node n) {
Stack<Node> stack = new Stack<>();
stack.push(n);
while(!stack.isEmpty()) {
Node curr = stack.pop();
System.out.println(curr.value); //调用都是意思意思
if(curr.right != null) stack.push(curr.right);
if(curr.left != null) stack.push(curr.left);
}
}
3. Lowest Common Ancestor
二叉树,给两个子节点,求他们的共同的,最小的,父节点
思路:因为是二叉树,只要找到一个点,数值在两个数之间久行。注意:这里虽然可以用recursion,但是因为recursion更适合在不同的branch内查询,或者查询node的规律,因此用iteration就可以遍历
public static Node minimumHeightA(Node n, Node v1, Node v2) {
int min = Math.min(v1.value, v2.value);
int max = Math.max(v1.value, v2.value);
while(n!= null) {
if(n.value > max) {
n = n.left;
}else if(n.value < min) {
n = n.right;
} else {
return n;
}
}
return null;
}
4. Binary Tree to Heap
Tree总结的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- 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 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Tree树节点选中及取消和指定节点的隐藏
指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...
随机推荐
- 大臣的旅费|2013年蓝桥杯A组题解析第十题-fishers
标题:大臣的旅费 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市 ...
- P1494 [国家集训队]小Z的袜子(莫队算法)
莫队板子 代码 #include <cstdio> #include <algorithm> #include <cstring> #include <cma ...
- (zhuan) 深度学习全网最全学习资料汇总之模型介绍篇
This blog from : http://weibo.com/ttarticle/p/show?id=2309351000224077630868614681&u=5070353058& ...
- GPIO实验之c语言
上一章节进行实验使用的是汇编进行编程的,本次实验是使用c语言进行编写的. (1)点亮一个led灯 1)启动文件: crt.S .text .global _start _star ...
- Win10下Java开发环境配置
首先下载符合操作系统版本的jdk,比如64位的JDK8: 下载链接:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-down ...
- Docker1之Container
Document An image is a lightweight, stand-alone, executable package that includes everything needed ...
- [0413] FFTSHIFT的四种写法
FFTSHIFT的四种写法 前言 matlab说,"你读过书,--我便考你一考.fftshift的函数,怎样写的?"我想,讨饭一样的人,也配考我么?便回过脸去,不再理会.matla ...
- ashx和aspx的区别
1. ashx是一般处理程序,一般返回的数据有两种,一种是html页面,一种是只返回一个字符串. 2. aspx是web窗体程序,每次新建都回自带一个界面和一个后台处理程序. 3. 根据以上两点,可以 ...
- [转]C和C++运行时库
转自csdn原文:https://blog.csdn.net/ithzhang/article/details/20160009 图片请去原文查看 在使用VC构建项目时,经常会遇到下面的链接错误: 初 ...
- R语言矩阵栅格显示矩阵颜色显示
效果如下: