#include #include#includeusing namespace std;//智二//交换数组中两个元素的位置void swap(int left, int right, int sort[]){ int temp; temp = sort[left]; sort...
分类:
编程语言 时间:
2015-04-05 20:11:23
阅读次数:
120
root指向一棵二叉树的头结点,p和q分别指向该二叉树中任意两个结点的指针,编写算法,找到p和q的最近公共祖先结点r。
为了不失一般性,设p在q的左边。同时,为了编写方便,将以p和q所指结点的值代替p和q本身。根据【后续遍历最后访问根节点,在递归算法中,根是压在栈底的】,很容易想到,采用后序遍历非递归算法:栈中存放二叉树结点的指针。当访问到某结点时,栈中所有的元素都是该结点的祖先。设一个辅助栈,当没...
分类:
其他好文 时间:
2015-04-04 21:18:20
阅读次数:
150
题目:
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].Note: Recursive solution is triv...
分类:
其他好文 时间:
2015-04-01 21:56:42
阅读次数:
96
斐波那契数列的递归实现:
public int fn(int n) {
if (n == 1 || n == 2)
return 1;
return fn(n-1)+fn(n-2);
}
非递归写法:
public int fn(int n) {
int a =1;
int b = 1;
int tmp;
if (n == 1 || n == 2)
return 1;
f...
分类:
编程语言 时间:
2015-04-01 15:26:53
阅读次数:
146
一、题目 要求输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建新的节点,只能调整树中结点指针的指向。 二叉树结点定义如下:1 struct BinaryTreeNode2 {3 int m_nValue;4 BinaryTreeNode *m...
分类:
编程语言 时间:
2015-03-31 17:19:41
阅读次数:
167
struct BinTree
{
int data;
BinTree * left;
BinTree * right;
};递归版本void PreOrder(BinTree * root)
{
if(root != nullptr)
{
cout <data;
PreOrder(root->left);...
分类:
其他好文 时间:
2015-03-30 18:48:42
阅读次数:
138
//////UserecursivemethodtoimplementFibonacci/////////staticintFn(intn){if(n46memorywilloverflow}递归算法时间复杂度是O(n2), 空间复杂度也很高的。当然不是最优的。自然我们想到了非递归算法了。一般的实现...
树的定义和基本术语树(Tree)是n(n>=0)个结点的有限集T,T为空时称为空树,否则它满足如下两个条件: (1)有且仅有一个特定的称为根(Root)的结点; (2)其余的结点可分为m(m>=0)个互不相交的子集T1,T2,T3…Tm,其中每个子集又是一棵树,并称其为子树(Subtree)。树形结...
分类:
其他好文 时间:
2015-03-30 17:49:32
阅读次数:
144
二叉排序树非递归插入代码 1 #include 2 #include 3 #include 4 #include 5 typedef struct node 6 { 7 int val; 8 struct node* lchild; 9 struct...
分类:
其他好文 时间:
2015-03-30 00:56:02
阅读次数:
178
求解二叉树的高度
树是递归定义的,所以用递归算法去求一棵二叉树的高度很方便。
#include
#include
using namespace std;
struct Node {
char data;
Node *lchild;
Node *rchild;
};
void High(Node *T, int &h)...
分类:
其他好文 时间:
2015-03-29 22:13:14
阅读次数:
239