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 ...
随机推荐
- SpringBoot 使用Sharding-JDBC进行分库分表及其分布式ID的生成
为解决关系型数据库面对海量数据由于数据量过大而导致的性能问题时,将数据进行分片是行之有效的解决方案,而将集中于单一节点的数据拆分并分别存储到多个数据库或表,称为分库分表. 分库可以有效分散高并发量,分 ...
- Docker 使用Dockerfile构建tomcat镜像
Dockerfile概念: 镜像的定制实际上就是定制每一层所添加的配置.文件.如果我们可以把每一层修改.安装.构建.操作的命令都写入一个脚本,用这个脚本来构建.定制镜像,那么之前提及的无法重复的问题. ...
- (zhuan) 资源|TensorFlow初学者必须了解的55个经典案例
资源|TensorFlow初学者必须了解的55个经典案例 2017-05-27 全球人工智能 >>>>>>欢迎投稿:news@top25.cn<<< ...
- Learning to Track at 100 FPS with Deep Regression Networks ECCV 2016 论文笔记
Learning to Track at 100 FPS with Deep Regression Networks ECCV 2016 论文笔记 工程网页:http://davheld.git ...
- jquery及jquery常用选择器使用
本文为博主原创,未经允许不得转载: 1.jquery强大之处: 容易上手,强大的选择器,解决浏览器的兼容 完善的时间机制,出色的ajax封装,丰富的ui 2.jquery是一个javas ...
- 5、lvs使用进阶(01)
四层.七层负载均衡的区别 https://jaminzhang.github.io/lb/L4-L7-Load-Balancer-Difference/ netfilter/iptables简介 ...
- HTML+CSS+JS 传智 详细笔记
HTML(1)- -毕向东老师对Html的简介 CSS- -毕老师对CSS的简介 Javascript- -毕老师对JS的简介 html&css等等练习表(W3Cscholl) js练习表回顾 ...
- Tomcat的manager app管理web项目
1.在浏览器地址栏输入http://localhost:8080/进入,如下图所示: 2.在点击Manager App 前,首次使用则需要对tomcat目录下的conf/tomcat-users.xm ...
- [从零开始搭网站六]为域名申请免费SSL证书(https),并为Tomcat配置https域名所用的多SSL证书
点击下面连接查看从零开始搭网站全系列 从零开始搭网站 由于国内的网络环境比较恶劣,运营商流量劫持的情况比较严重,一般表现为别人打开你的网站的时候会弹一些莫名其妙的广告...更过分的会跳转至别的网站. ...
- 无视编码都统一转成unicode 然后截断 例如 。“发发发发发发” 操作之后显示为 “发发发发...”
-- local function checkPlayName( str ) -- str = Utils.utf8_to_unicode(str)-- local retStr = "&q ...