二叉树的前序遍历:root点先被访问,然后是left和right子节点。迭代的版本也相对好写。1、递归版本:时间复杂度O(N),空间复杂度O(N) 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int va...
分类:
其他好文 时间:
2014-07-07 20:35:24
阅读次数:
158
二叉树的中序遍历1、递归版本/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) :...
分类:
其他好文 时间:
2014-07-07 20:00:20
阅读次数:
161
二叉树的后续遍历1、递归版本/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) :...
分类:
其他好文 时间:
2014-06-30 14:17:28
阅读次数:
306
一直想要写的 二叉树 中序 先序 后序遍历算法
递归的太简单了,就不写了。关键是非递归版本。
先序:
我自己的版本:
void RootPreTraverse(Node* p)
{
Stack S;
while(S not empty)
{
p=S.top();
S.pop();
Show(p);
if(p->right!=null)
S...
分类:
其他好文 时间:
2014-06-20 10:55:49
阅读次数:
279
#includeint MinSubsequenceSum(const int A[],int n){ int i,sum,MinSum; sum=MinSum=0; for(i=0;i0) sum=0; } return MinSum;}void...
分类:
其他好文 时间:
2014-06-18 17:45:03
阅读次数:
136
简单来说,就是二叉树的前序、中序、后序遍历,包括了递归和非递归的方法前序遍历(注释中的为递归版本): 1 #include 2 #include 3 #include 4 #include 5 6 using namespace std; 7 8 struct TreeNode 9 {1...
分类:
其他好文 时间:
2014-06-17 12:53:42
阅读次数:
416
前中后遍历 递归版 1 /* Recursive solution */ 2 class
Solution { 3 public: 4 vector preorderTraversal(TreeNode *root) { 5 6 vector
resul...
分类:
其他好文 时间:
2014-06-10 19:38:03
阅读次数:
219
本文分别用非递归和递归两种方式实现了链表的反转,在九度OJ上测试了非递归版本,AC。
题目描述:
输入一个链表,反转链表后,输出链表的所有元素。
(hint : 请务必使用链表)
输入:
输入可能包含多个测试样例,输入以EOF结束。
对于每个测试案例,输入的第一行为一个整数n(0<=n<=1000):代表将要输入的链表的个数。
输入的第二行包含n个整数t(0<=t<=1000000):代表链表元素。
输出:
对应每个测试案例,
以此输出链表反转后的元素,如没有元素则输出NULL。
样例输入:...
分类:
其他好文 时间:
2014-05-15 06:41:12
阅读次数:
315
非递归版好像被卡掉了,其他2个板子都能过。
#include
#include
#include
#include
#include
using namespace std;
#define ll int
const int MAXN = 100100;//点数的最大值
const int MAXM = 400010;//边数的最大值
const int INF = 0x3f3f3f3f;
st...
分类:
其他好文 时间:
2014-05-09 22:55:23
阅读次数:
476
递归版
struct Edge
{
int from, to, cap, flow;
Edge(){}
Edge(int from, int to, int cap, int flow) : from(from), to(to), cap(cap), flow(flow){}
};
int n, m, s, t;
vector edges;
vector G[maxn];
bool v...
分类:
其他好文 时间:
2014-05-06 23:39:25
阅读次数:
351