树结构问题因为容易写出解法,因此经常出现在面试题中

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总结的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  3. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  10. Tree树节点选中及取消和指定节点的隐藏

    指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...

随机推荐

  1. utf-8并不"兼容" gb2312, gb18030

    注意 utf-8 并不是 向下 兼容"gb2312 gb18030"等编码, 也并不是说, utf-8就是比 gb2312等高级的编码! 比如在terminal中, 你开始使用的 ...

  2. P4556 [Vani有约会]雨天的尾巴

    目录 思路 优化 过程中的问题/疑问 错误 代码 思路 每个节点维护一课线段树(当然是动态开点) 线段树的作用是统计这个节点有多少种粮食型号,以及最多的粮食型号 然后树上差分,u和v点 +1,lca( ...

  3. c 语言中宏定义和定义全局变量的区别

    宏定义和定义全局变量的区别: 1 作用时间不同. 宏定义在编译期间即会使用并替换,而全局变量要到运行时才可以. 2 本质类型不同. 宏定义的只是一段字符,在编译的时候被替换到引用的位置.在运行中是没有 ...

  4. Python中的open和codecs.open

    最近老被编码困扰,多次折腾之后,感觉python的编解码做得挺好的,只要了解下边的流程,一般都能解决 input文件(gbk, utf-8...) ----decode-----> unicod ...

  5. NLP--- How to install the tool NLTK in Ubuntu ?

    NLP--- How to install the tool NLTK in Ubuntu ? 1. open the website of NLTK and download it.  https: ...

  6. (转载)C#控件缩写规范

    标准控件缩写规范 类 型 前 缀 示 例 Adrotator adrt adrtTopAd BulletedList blst blstCity Button btn btnSubmit Calend ...

  7. C++笔记(2018/2/7)

    类class 类的名字就是用户自定义的类型的名字.可以像使用基本类型那样来使用它. 一个类所占用的内存空间的大小,等于所有成员变量的大小之和. 类之间可以用 "="进行赋值,但是不 ...

  8. select2 使用方法总结

    官网:http://select2.github.io/ 调用 <link href="~/Content/select2.min.css" rel="styles ...

  9. 批量Excel数据导入Oracle数据库 导入excel错误:外部表不是预期的格式 解决方案

    在asp.net网站中导出Excel文件后,再把文件导入到数据库中. 读取Excel文件时,打开连接出错. 错误为:外部表不是预期的格式 解决:检查了一下,导出的Excel是标准文件不是html,没错 ...

  10. <aop:aspect>与<aop:advisor>的区别

    在开发过程中,不少有Spring Aop的使用,在面向切面编程时,我们会使用< aop:aspect>:在进行事务管理时,我们会使用< aop:advisor>.那么,对于&l ...