树
jiyanfeng1
喜欢算法和编程的工科男
展开
-
[LeetCode] Populate the next right pointer in binary tree I
Populate the next right pointer in binary treeGiven the structure of node in binary treestructNode { Node* leftChild; Node* rightChild; Node* nextRight;}Populate the nextRight pointer原创 2013-02-27 11:38:31 · 721 阅读 · 0 评论 -
[LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2014-12-05 07:34:09 · 541 阅读 · 0 评论 -
[LeetCode] 一个BST的两个节点的值被交换,要求修正这个BST
给你一个搜索二叉树,如下: 6 / \ 10 2 / \ / \1 3 7 12可以看到 10 和 2 的位置发生了交换。要求设计算法,回复原来的BST。思路:可以借助 isValidBST 的思想,找出被交换的两个节点 。找到之后,原创 2013-02-24 00:48:31 · 1619 阅读 · 0 评论 -
判断整数序列是不是二元查找树的后序遍历结果
判断整数序列是不是二元查找树的后序遍历结果题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果: 8 / \ 6 10 / \ / \ 5 7 9 11因此原创 2012-09-13 11:39:17 · 542 阅读 · 0 评论 -
二叉树最大路径和 Binary Tree Max Path Sum
本题有两个相关问题:1)所求路径可以位于任意两个节点之间;2)所求路径位于两个叶子节点之间。下面给出问题1)的解法。问题2)的解法可以根据解法1)很容易得出。问题1)的解法:遍历所有节点,求出经过每一个节点的最大路径,最后再求出经过每个节点的最大路径的最大值。用递归的思路,自底向上递归。下面是代码: void maxPathSumUtil(Node* root, int& l原创 2014-12-02 06:49:58 · 2157 阅读 · 0 评论 -
在二叉树中,距离最远的两个节点的距离
在二叉树中,找到距离最远的两个节点的距离在二叉树中,找到距离最远的两个节点的距离。在上面的二叉树中,最远的节点的距离是:4(路径是2-3-13-5-2)。解决思路:递归。最远的两个节点,要么都在根节点的左子树,要么在都在根节点的右子树,要么分别在左子树和右子树,还有可能是深度最深的节点和根节点距离最远。代码如下:int longest_dis(N原创 2012-09-06 14:11:18 · 7182 阅读 · 0 评论 -
从根到叶子的和 sum root to leaf numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the tota原创 2014-12-03 06:26:54 · 604 阅读 · 0 评论 -
[LeetCode] flatten binary tree 扁平化二叉树
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原创 2014-12-03 10:27:24 · 1500 阅读 · 1 评论 -
Remove all nodes which don’t lie in any path with sum>= k
Given a binary tree, a complete path is defined as a path from root to a leaf. The sum of all nodes on that path is defined as the sum of that path. Given a number K, you have to remove (prune the tre原创 2014-12-17 14:08:59 · 751 阅读 · 0 评论 -
克隆一个有随机指针的二叉树 Clone a Binary Tree with Random Pointers
一个二叉树的节点有如下结构struct node { int key; struct node *left,*right,*random;}翻译 2014-11-25 10:19:29 · 930 阅读 · 0 评论 -
把二叉树的叶子节点连成一个二叉链表
把二叉树的叶子节点连成一个二叉链表原创 2014-11-25 05:14:15 · 1503 阅读 · 0 评论 -
[LeetCode] 路径和 Path Sum
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 / \原创 2014-12-04 23:40:29 · 571 阅读 · 0 评论 -
[LeetCode] 由前序和中序序列,构建二叉树
相关问题:从BST的前序遍历序列恢复该BST已知二叉树的前序遍历序列和中序遍历序列,要求重新恢复该二叉树。#include #include #include #include using namespace std; struct Node{ char key; Node* left; Node* right; Node(char k, Node原创 2013-02-23 05:31:34 · 1476 阅读 · 0 评论 -
[LeetCode] Populate the next right pointer in binary tree II
这篇文章给出的算法,只适用于complete binary tree;如果是任意形状的二叉树,则不适用。下面给出一个算法,适用于任意形状的二叉树。算法一:思路见geekforgeeks。该算法被leetcode给出了超时的错误。void connect(TreeLinkNode *root) { // main function if(root==NULL)原创 2014-12-06 12:04:42 · 526 阅读 · 0 评论 -
[LeetCode] 从排序的单链表到平衡搜索二叉树
给你一个排过序的单链表,构建一个平衡的搜索二叉树。In this method, we construct from leaves to root. The idea is to insert nodes in BST in the same order as the appear in Linked List, so that the tree can be constructed in原创 2013-02-24 11:17:56 · 556 阅读 · 0 评论 -
后缀树和前缀树
给你一个很长的字符串 s,和一堆短字符串 T = {t1, t2, ...}。 设计一个高效的算法,确定T 里的每个字符串是不是 s 的子串。思路:先对字符串s构建一个后缀树,然后看看t1,t2...,是不是s的一个后缀的前缀。构建后缀树和查找后缀树的前缀都可以用递归的思路来解决。具体代码如下:public class SuffixTree { SuffixTreeNode ro原创 2015-07-31 11:12:05 · 872 阅读 · 0 评论 -
Find the distance between two nodes
找出二叉树中两个结点间的距离.下面的算法的思路和lowest common ancestor的思路是一样的。适用的情况是,给定的两个节点都存在于二叉树上,而且一个节点不能是另外一个的父节点。#include #include using namespace std;struct Node{ int data; Node* left; Node* right; Nod原创 2015-01-17 05:29:05 · 656 阅读 · 0 评论 -
打印距离叶子节点为k的节点 Print nodes that are at distance k from a leaf node
Print all nodes that are at distance k from a leaf nodeGiven a Binary Tree and a positive integer k, print all nodes that are distance k from a leaf node.Here the meaning of distance is different翻译 2014-12-11 05:51:54 · 878 阅读 · 0 评论 -
Boundary Traversal of binary tree
Boundary Traversal of binary treeGiven a binary tree, print boundary nodes of the binary tree Anti-Clockwise starting from the root. For example, boundary traversal of the following tree is “20原创 2013-02-24 01:15:57 · 645 阅读 · 0 评论 -
Light is falling on a tree from left side you have to find all the nodes on which this light will fa
思路:所求节点就是层序遍历中,每一层的第一个节点。#include #include #include // pairusing namespace std;struct Node{ int data; Node* left; Node* right;};vector lightFall(Node* root){原创 2015-01-16 06:26:21 · 1080 阅读 · 0 评论 -
[LeetCode] Nonrecursive postorder traversal 非递归后续遍历
简单的做法是,用一个栈来保存遍历时的节点,用另外一个栈保存经过节点的次数。下面的算法不需要节点次数的栈。翻译自:http://leetcode.com/2010/10/binary-tree-post-order-traversal.htmlWe use a prev variable to keep track of the previously-traversed no翻译 2014-12-09 01:24:42 · 908 阅读 · 0 评论 -
[LeetCode] 非递归中序遍历二叉树 non recursive inorder traversal
非递归中序遍历二叉树 non recursive inorder traversal 用两个 while 循环来做, 思路如下.1) Create an empty stack S.2) Initialize current node as root3) Push the current node to S and set current = current->left until原创 2014-11-27 00:30:00 · 1261 阅读 · 0 评论 -
[LeetCode] Nonrecursive preorder traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].下面给出非递归前序遍历的算法,和非递归中序遍历非常像。原创 2014-12-08 12:39:27 · 580 阅读 · 0 评论 -
[LeetCode] Unqiue Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, given n = 3, your program should return all 5 unique BST's shown below. 1 3原创 2014-12-07 06:32:55 · 499 阅读 · 0 评论 -
子序列的最大几个值 maximum k elements in a subarray
子序列的最大几个值 maximum k elements in a subarray原创 2014-11-24 07:18:02 · 557 阅读 · 0 评论 -
二叉树的高度 - 父节点序列 Find Height of Binary Tree by Parent Array
二叉树的高度 - 父节点序列 Find Height of Binary Tree by Parent ArrayA given array represents a tree in such a way that the array value gives the parent node of that particular index. The value of the root node翻译 2014-11-29 03:47:24 · 642 阅读 · 0 评论 -
lowest common ancestor 最低公共祖先
lowest common ancestor of binary tree转载 2013-02-25 12:20:57 · 571 阅读 · 0 评论 -
用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(一)
用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(二)用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 通常情况下,我们是用数组来实现完全二叉树的。如果parent node的下标是 i,那么左孩子、右孩子的下标分别是 2*i+1、2*i+2。但是这篇文章将介绍如何用二叉链表实现完全二叉树。Ho翻译 2013-02-22 01:26:23 · 1076 阅读 · 0 评论 -
找到二叉树的Ceil值
给你一个二叉树和一个数值,找出二叉树中比这个数值大的最小的值(也就是所谓的Ceil值)。代码如下:#include #include /* A binary tree Node has key, left child and right child */struct Node{ int key; Node* left; Node* right;原创 2013-02-22 01:07:02 · 618 阅读 · 0 评论 -
用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(二)
用二叉链表实现完全二叉树 (Linked Complete Binary Tree) 的实现(一)本文采用STL来实现队列,并用这个队列实现Linked Complete Binary Tree的插入函数。代码非常简洁,代码如下:#include #include #include using namespace std;struct Node{ int key;原创 2013-02-22 02:36:34 · 1168 阅读 · 0 评论 -
线索二叉树 Threaded Binary Tree (不用递归,不用栈,遍历二叉树)
线索二叉树 Threaded Binary Tree不用递归,不用栈,遍历二叉树Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary Tree. In原创 2013-02-21 13:08:08 · 668 阅读 · 0 评论 -
改二叉树为链表,使每个节点指向其中序遍历的下个节点
改二叉树为链表,使每个节点指向其中序遍历的下个节点用递归的方法。(以下代码已经过测试)struct node *btree2list(struct node *tree) { struct node *right; struct node *left; struct node *list=NULL; struct node *tmp; if(tree) { right =原创 2013-02-06 15:07:40 · 523 阅读 · 0 评论 -
给定一个数组,写个程序构造一个最矮的二叉树
思路:尽量保证左子树和右子树含有相同数目的节点。1.用数组中间元素构造根节点;2.用中间元素左边的元素构造左子树;3.用中间元素右边的 元素构造右子树。代码:下面的代码给出了两种实现的函数:create_tree1()和create_tree2()。读者可以观察一下二者的区别。#include#include#include#includeusing names原创 2013-01-16 13:03:34 · 1238 阅读 · 0 评论 -
非递归后序遍历二叉树
非递归前序遍历二叉树,请看这里。非递归后序遍历二叉树:遍历时,除了用栈保存子树根节点,还要保存对子树根节点的访问次数。#include#include #include#includeusing namespace std;struct node{ node* left; node* right; int value; node(node* l, node* r原创 2013-01-15 13:02:30 · 755 阅读 · 0 评论 -
整数序列是不是二叉查找树的后序遍历结果
题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:8/ \6 10/ \ / \5 7 9 11因此返回true。如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。分析:这是一道trilo转载 2012-10-19 06:05:16 · 2803 阅读 · 0 评论 -
Optimal BST
Optimal BSTAssume:We have a list of n items: a1, a2, ..., anKey(ak) = akProbability of accessing item ak is known in advance and is P(ak)The list is ordered by keys, a1转载 2012-10-18 06:14:10 · 3503 阅读 · 0 评论 -
中序遍历二叉树
给定一个二叉树,允许的操作有Tree-Min()和Tree-Successor(x).Tree-Min()返回整个树的最小节点,Tree-Successor(x)返回节点x的下一个中序遍历节点。要求,用这两种操作对二叉树进行中序遍历。解答:先用Tree-Min()找到最小节点,然后连续调用Tree-Successor(x),直到结束。这样就完成了二叉树的中遍历。原创 2012-10-11 06:45:04 · 831 阅读 · 0 评论 -
Boundary Traversal of binary tree
Boundary Traversal of binary treeGiven a binary tree, print boundary nodes of the binary tree Anti-Clockwise starting from the root. For example, boundary traversal of the following tree is “20转载 2013-02-24 01:17:11 · 522 阅读 · 0 评论 -
判断一个二叉树是否是完全二叉树
判断一个二叉树是否是完全二叉树思路:在层序遍历的过程中,找到第一个非满节点(non-full node)。满节点(full-node)指的是同时拥有左右孩子的节点。在找到第一个非满节点之后,剩下的节点不应该有孩子节点;如果有,那么该二叉树就不是完全二叉树。代码如下:bool isCompleteTree(Node* root){ queue que; que.push(root);原创 2013-02-24 08:22:34 · 3315 阅读 · 0 评论 -
由前序遍历构建一个特殊的二叉树
一个二叉树的每个节点,要么有两个孩子,要么没有孩子。给你一个数组 int pre[],该数组代表这个二叉树的前序遍历序列;数组 char preLN[] 代表相应的节点是不是叶子节点,'L'表示是叶子节点,'N'表示非叶子节点。要求,恢复出原来的二叉树。思路:可以采用和 “从前序遍历序列恢复BST(线性时间复杂度)” 相似的思想来做这道题。代码如下:struct Node{原创 2013-02-24 09:38:42 · 571 阅读 · 0 评论