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

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. 再谈 linux 的sed用法

    很多东西, 如果太复杂, 太庞杂, 一开始, 可以只掌握最简单的. 如果连最简单的都不能掌握, 那看那么多有什么用? 关于cut, sed的 处理过程和思想? vm: virtual machine, ...

  2. oracle 之 定时任务,存储过程和游标等语法案例

    --定时任务 declare job20 number; begin sys.dbms_job.submit(job20,'test1;',sysdate,'sysdate+1'); end; --存 ...

  3. Unity3D学习笔记(二十八):Editor

    Editor: 对编辑器进行一些拓展开发 关于继承Mono类的扩展开发   特性: [特性]:声明性的标签(类,方法,结构体,变量) 特性只对字段声明有效,后面必须接字段:多个特性,可以修饰一个字段 ...

  4. MPI 环境搭建问题-运行程序闪退

    安装后smpd无法运行,进程中没有smpd.exe.注册过程也完成了.运行自带的测试程序cpi.exe,提示:Error: No smpd passphrase specified through t ...

  5. 【NOIP 2016】Day2 T3 愤怒的小鸟

    Problem Description \(Kiana\) 最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 \((0,0)\) 处,每次 \(Kiana\ ...

  6. 【译】第45节---EF6-索引属性

    原文:http://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx Entity ...

  7. 51nod 1351 吃点心(贪心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351 题意: 思路: 要么先选low值大的,要么先选high值大的,分两 ...

  8. go 一波走起

    $ go run helloworld.go 运行 $ go build helloworld.go 编译 该命令生成一个名为helloworld的可执行的二进制文件,可以随时运行它 $ ./hell ...

  9. js全选 反选

    // 全选 反选 allChoose: function (o) { var obj = $.extend(true, { id: "#id", name: "name& ...

  10. java反编译器

    一时手残,把java工程中的源文件给删了,幸亏还有.class文件,想起java可以反编译,所以试一试. JD-Eclipse 如果是使用Eclipse的话,可以用Eclipse插件JadClipse ...