二叉树 (binary tree)
文章平均质量分 72
beiyetengqing
http://blog.youkuaiyun.com/beiyeqingteng 的镜像站
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
找出二叉查找树中第n大的值
问题:给一个二叉查找树(BST),找出第 k 大的值。比如:该图中,第3大的值是10.分析:我们可以通过类似中序遍历的方法把BST从大到小排序,然后,就可以得到第 k 大的值了。代码如下:public class NthNode { // k refers to the counter. it is a global variable. st原创 2012-06-04 10:21:57 · 1385 阅读 · 0 评论 -
Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.Code:/原创 2012-12-13 10:50:44 · 1085 阅读 · 0 评论 -
BK-Trees
BK-Trees, or Burkhard-Keller Trees are a tree-based data structure engineered for quickly finding near-matches to a string, for example, as used by a spelling checker, or when doing a 'fuzzy' search转载 2012-12-28 02:21:40 · 1104 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.原创 2012-12-31 13:09:43 · 1901 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for binary tree * public class Tr原创 2013-01-22 08:26:34 · 1392 阅读 · 0 评论 -
Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2013-01-23 12:32:04 · 1534 阅读 · 0 评论 -
二叉树非递归遍历
这里写出三种儿叉查询树遍历的非递归写法,非常有意思。preorder:先打印root,再left,最后right。 public static void BSTPreorderTraverse(Node node) { if (node == null) { return; } Stack s = new Stack(); s.原创 2013-02-16 07:15:44 · 1024 阅读 · 0 评论 -
print out the path from one node to another in binary tree
Question:You are given one binary tree, find the path from one node to another.public static LinkedList printPath(Node root, Node n1, Node n2) { Node temp = root; LinkedList rootToN1 = new Lin原创 2013-02-18 11:33:56 · 784 阅读 · 0 评论 -
打印二叉树所有的路径
问题:给一个二叉树,把所有的路径都打印出来。比如,对于下面这个二叉树,它所有的路径为:8 -> 3 -> 18 -> 2 -> 6 -> 48 -> 3 -> 6 -> 78 -> 10 -> 14 -> 13思路:从根节点开始,把自己的值放在一个数组里,然后把这个数组传给它的子节点,子节点同样把自己的值放在这个数组里,又原创 2012-06-04 10:12:32 · 696 阅读 · 0 评论 -
Path Sum II
Question:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5原创 2012-12-09 12:52:50 · 1544 阅读 · 0 评论 -
check a tree is balanced or not
Question:Given a binary tree, check whether it is balanced or not.Analyze:Consider a height-balancing scheme where following conditions should be checked to determine if a binary tree is bal原创 2012-06-06 13:34:45 · 845 阅读 · 0 评论 -
Sum of a tree
You are given a tree, and the nodes in the tree may have more than two child nodes, calculate the sum of the tree where the root to the leaf is considered as a number.public class TreeSum { priva原创 2013-02-22 07:40:36 · 632 阅读 · 0 评论 -
Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f原创 2012-12-11 06:07:16 · 4089 阅读 · 8 评论 -
closest node to the target in BST
Question:Give a BST, find a node whose value is closest to the target.// when calling this method, set minDiff = Integer.MAX_VALUE, prev = null;public static Node closestValue(Node root, int targ原创 2012-12-20 00:03:27 · 685 阅读 · 0 评论 -
检查一个数组是否可能是一棵二叉查询树后序遍历的结果
问题:给一个数组,检查它是否可能是一棵二叉查询树后序遍历的结果。换句话说,是否存在一棵二叉查询树,对它进行后序遍历后,得到的数组和给定的数组完全一样。比如,对上面这个二叉查询树后序遍历后,得到的数组是:[1, 4, 7, 6, 3, 13, 14, 10, 8]。思路:因为二叉树后序遍历的时候,先左后右然后中间。所以,数组的最后一个一定是整个二叉查询树的根(比如 8),而且原创 2012-06-06 05:02:50 · 798 阅读 · 0 评论 -
Convert a BST to a sorted doubly-linked list in-place
Convert a BST to a sorted doubly-linked list in-placepublic class Solution { static Node lastNode = null; public static void main(String[] args) { Node n1 = new Node(7); Node n2 = new No原创 2013-02-02 08:53:08 · 770 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
Question:Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3原创 2012-12-10 00:13:58 · 3195 阅读 · 0 评论 -
Path Sum
Question:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binar原创 2012-12-09 11:03:47 · 644 阅读 · 0 评论 -
二叉树及其基本操作
二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树:若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于或者等于它的根结点的值;它的左、右子树也分别为二叉排序树。二叉查找树的表示:class Node { int data; Node leftChild =原创 2012-06-03 06:59:44 · 678 阅读 · 0 评论 -
判断一个树是不是二叉查询树
问题:给定一个二叉树,判断它是否是二叉查询树。思路:要判断是否是二叉查询树,标准就是看每一个节点是否满足:1、左节点及以下节点的值比它小;2、右节点及以下节点的值比它大。当然,前提是子节点都存在的情况。所以,我们需要从根节点不断向下递归,只要所有节点都满足,那么就是BST,否则,就不是。代码:private boolean isBST(Node node)原创 2012-06-04 10:04:59 · 707 阅读 · 0 评论 -
最近公共祖先 (Lowest common ancestor)
问题:给定一个二叉树,找到两个节点NA, NB的最近公共祖先(LCA)。比如对于下图,4 和 7 的 LCA 是6, 1和13的LCA 是 8。分析:我们这里先考虑一般的二叉树(BT),然后再考虑这个二叉树是二叉搜索树(BST)的情况。查找两个node的最早的公共祖先,分三种情况:1. 如果两个node在root的两边,那么最早的公共祖先就是root。原创 2012-06-05 11:07:59 · 6398 阅读 · 2 评论 -
把一个二叉树转成它的“镜像”
问题:把一个二叉树转成它的“镜像”(mirror),例子如下所示:分析:先交换左右子树的根节点,然后递归到下一层,直到根节点为null.代码:void mirror(Node rootNode) { if (rootNode != null) { // swap the left/right pointers Node tem原创 2012-06-06 13:22:04 · 1660 阅读 · 0 评论 -
给定一个二叉树,从左到右,找出第 k 个叶子节点
问题:给定一个二叉树,从左到右,找出第 k 个叶子节点。比如图中二叉树的第 3 个叶子节点(从左到右)是 11.分析:因为顺序是从左往右数,所以,对于一个节点下的两个叶子节点来讲(比如 6 下面有两个叶子节点 5 和 11),我们要确保先遍历最左边一个,然后再遍历右边一个。这样,其实,在中序遍历,前序遍历和后序遍历中,都能保证左边叶子节点比右边叶子节点先原创 2012-06-08 04:44:21 · 1672 阅读 · 0 评论 -
convert a sorted array into a balanced binary search tree
Question:Given a sorted array, and converted it into a balanced binary search tree. Solution:If you would have to choose an array element to be the root of a balanced BST, which element woul原创 2012-06-08 05:26:14 · 717 阅读 · 0 评论 -
The diameter of a binary tree
Definition: The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two leaves in the tree.The diameter of a tree T is the largest of the following q原创 2012-06-21 20:26:13 · 772 阅读 · 0 评论 -
打印二叉树的边缘节点
问题:给你一个complete 二叉树,逆时针打印所有边缘节点, 比如:那么,逆时针打印边缘节点后,输出:1 , 3, 5, 9, 8, 6 .分析:如果想写一个方法实现这个要求是很难的,但是,我们可以考虑分步实现。第一步:打印左边的边缘节点;第二步:打印底部所有节点;第三步:打印右边所有边缘节点。代码如下:public static原创 2012-07-01 01:01:47 · 2064 阅读 · 0 评论 -
ZigZag打印二叉树
问题:给定二叉树,ZigZag打印每一层的节点,如果上一层是从左到右,下一层就是从右到左。分析:本题和分层打印二叉树是一样的,我们只需要判断上一层的顺序,就可以判断该层的顺序。代码:public static void printZigZag(Node node) { ArrayList list1 = new ArrayList(); ArrayList原创 2012-07-01 12:06:52 · 2017 阅读 · 0 评论 -
分层打印二叉树
问题:把一个二叉树,安装从root到leaf的顺序把每一层上的node从左到右打印出来。分析:利用两个arraylist,一个arraylist装上一层的node, 另一个arraylist装上一层的child。如果上一层arraylist空了,两个arraylist互换。代码://print the binary tree by levelpublic stat原创 2012-06-04 10:29:53 · 921 阅读 · 0 评论 -
Trie (prefix tree) 实现 (Java)
关注Trie 这种结构已经很久,Trie有一个很有趣的用途,那就是自动提示。而且,前不久在一次面试里,也需要用Trie来解答。所以,在此对这个数据结构进行总结。Trie,又称单词查找树或键树,是一种树形结构。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。它有3个基本性原创 2012-08-12 05:07:19 · 8009 阅读 · 0 评论 -
二叉查询树的保存和读取
问题:把一个二叉查询树保存到一个文件,然后通过这个文件把这个二叉查询树还原出来。我们使用pre-order遍历把一个二叉查询树保存,原因是只有pre-order遍历是从root开始保存,这样,当我们读取的时候,才能够把那个值放在root。这里我用print来表示保存。public void preOrderWrite(Node root) { if (root!= null) {原创 2012-06-06 12:47:34 · 1918 阅读 · 0 评论 -
二叉查询树的保存和读取
问题:把一个二叉查询树保存到一个文件,然后通过这个文件把这个二叉查询树还原出来。我们使用pre-order遍历把一个二叉查询树保存,原因是只有pre-order遍历是从root开始保存,这样,当我们读取的时候,才能够把那个值放在root。这里我用print来表示保存。public void preOrderWrite(Node root) { if (原创 2012-08-17 09:51:51 · 1704 阅读 · 0 评论 -
把二叉查找树转变成排序的双向链表
问题:给一个二叉查找树,把它转化成排序的双向链表,要求是不能使用额外的空间(常数空间是允许的)。例如:该二叉查找树的双向链表结构是 1 = 3 = 4 = 6 = 7 = 8 = 10 = 13 = 14分析:二叉查找树具有非常鲜明的递归结构,因此,在解决这道问题时,我们无可避免的会想到用递归。关键是如何递归呢?因为一个节点总是有一个左节点和右节点(如果存在),对于原创 2012-08-29 23:02:16 · 938 阅读 · 0 评论 -
从大到小打印BST的值
问题:给定一个BST, 从大到小打印里面的值。分析:我们知道通过inorder,我们可以从小到大打印BST的值。同理,为了能够从大到小打印,我们首先应该打印“根”节点的右子树的值,然后打印“根”节点,然后打印左子树的值。代码:public static void printTreeByDecreasingOrder(Node root) { if (root != null)原创 2012-12-02 07:00:28 · 1455 阅读 · 0 评论 -
不使用栈把二叉树中序输出
问题:把一个二叉树中序输出,但是不能使用栈或者O(n)空间来实现。分析:把二叉树中序输出,我们可以使用递归或者采用非递归方式,但是不管是哪种方式,我们总是需要O(n)的空间,所以如果只能使用常数空间,的确很难想到,但是一旦掌握该种方法,还是挺有用的,至少拓宽了自己解决问题的思路。对于中序遍历,我们总是先输出左子树部分,再root,最后才是右子树部分。对于左子树和右子原创 2015-08-13 00:12:40 · 696 阅读 · 0 评论
分享