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 ...
随机推荐
- luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题目链接 luogu 思路 可以说是线段树合并的练手题目吧 也没啥说的,就是dfs,然后合并... 看代码吧 错误 和写主席树错的差不多 都是变量写错.... 代码 #include <bits ...
- 剥开比原看代码11:比原是如何通过接口/create-account创建帐户的
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...
- 【C#】扩展方法浅谈
C#3 引入的扩展方法这一个理念. 扩展方法最明显的特征是在方法参数中第一个参数有this声明. 其实C#库中有很多已经是扩展方法了.比如linq中对序列使用的查询语句, where, select等 ...
- 授权oAuth
使用Client Credentials Grant授权方式给客户端发放access token 只验证客户端(Client),不验证用户(Resource Owner),只要客户端通过验证就发acc ...
- hihoCoder 1515 分数调查(带权并查集)
http://hihocoder.com/problemset/problem/1515 题意: 思路: 带权并查集的简单题,计算的时候利用向量法则即可. #include<iostream&g ...
- HDU 4318 Power transmission(最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=4318 题意: 给出运输路线,每条路线运输时都会损失一定百分比的量,给定起点.终点和初始运输量,问最后到达终点时最 ...
- java web 工程更改名字
如图: 将工程名字struts2Project02更改为struts2Project03,步骤如下: 1. 右键工程名字,选中properties,如图 2.更改项目名字 3.第2步已经真正把项目名字 ...
- JavaSE习题 第七章 常用实用类
问答题 1.怎样实例化一个Calendar对象? Calendar ca=Calendar.getInstance(); 2.Calendar对象调用set(1949,9,1)设置的年月日分别是多少? ...
- 使用bat文件执行sql文件
test.bat mysql -uroot -p[password] < test.sql pause test.sql CREATE DATABASE IF NOT EXISTS test_d ...
- cordova + Vue 开发 APP 上手指南
什么是 cordova cordova 是由 Apache 基金会支持的,使用 HTML5 + CSS3 + JS 来构建多平台 APP 程序的开发框架.其支持调用手机系统(Android.IOS.W ...