111,090
社区成员




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static int FibonacciRecursively(int n)
{
if (n < 2) return n;
int t1 = 0;
t1 = FibonacciRecursively(n - 1);
return t1 + FibonacciRecursively(n - 2);
}
static void Main(string[] args)
{
Console.WriteLine(FibonacciRecursively(10));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static int FibonacciRecursively(int n)
{
Stack<Tuple<int, int, int>> stack = new Stack<Tuple<int, int, int>>(); // addr, n, t1
int result = 0;
int t1 = 0;
begincall:
if (n < 2)
{
result = n;
goto endcall;
}
stack.Push(new Tuple<int, int, int>(1, n, t1));
n = n - 1;
t1 = 0;
goto begincall;
returncall1:
t1 = result;
stack.Push(new Tuple<int, int, int>(2, n, t1));
n = n - 2;
t1 = 0;
goto begincall;
returncall2:
result = t1 + result;
goto endcall;
endcall:
if (stack.Count == 0) return result;
Tuple<int, int, int> context = stack.Pop();
n = context.Item2;
t1 = context.Item3;
if (context.Item1 == 1)
goto returncall1;
else
goto returncall2;
}
static void Main(string[] args)
{
Console.WriteLine(FibonacciRecursively(10));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static int FibonacciRecursively(int n)
{
Stack<Tuple<int, int, int>> stack = new Stack<Tuple<int, int, int>>();
int t1 = 0;
int result = 0;
while (true)
{
bool b = false;
if (n < 2)
{
result = n;
while (stack.Count != 0)
{
Tuple<int, int, int> context = stack.Pop();
n = context.Item2;
t1 = context.Item3;
if (b = context.Item1 == 1)
{
t1 = result;
stack.Push(new Tuple<int, int, int>(2, n, t1));
n = n - 2;
t1 = 0;
break;
}
result = t1 + result;
}
if (b) continue; else break;
}
stack.Push(new Tuple<int, int, int>(1, n, t1));
n = n - 1;
t1 = 0;
}
return result;
}
static void Main(string[] args)
{
Console.WriteLine(FibonacciRecursively(10));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static int FibonacciRecursively(int n, Stack<Tuple<int, int, int>> stack, int t1, int result)
{
start:
bool b = false;
if (n < 2)
{
result = n;
while (stack.Count != 0)
{
Tuple<int, int, int> context = stack.Pop();
n = context.Item2;
t1 = context.Item3;
if (b = context.Item1 == 1)
{
t1 = result;
stack.Push(new Tuple<int, int, int>(2, n, t1));
n = n - 2;
t1 = 0;
break;
}
result = t1 + result;
}
if (b) goto start; else return result;
}
stack.Push(new Tuple<int, int, int>(1, n, t1));
n = n - 1;
t1 = 0;
return FibonacciRecursively(n, stack, t1, result);
}
static void Main(string[] args)
{
Console.WriteLine(FibonacciRecursively(10, new Stack<Tuple<int,int,int>>(), 0, 0));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class TreeNode
{
public TreeNode(int value, TreeNode left, TreeNode right)
{
this.Value = value;
if (left != null)
left.Parent = this;
this.Left = left;
if (right != null)
right.Parent = this;
this.Right = right;
}
public int Value { get; private set; }
public TreeNode Left { get; private set; }
public TreeNode Right { get; private set; }
public TreeNode Parent { get; private set; }
}
class Program
{
public static void PreOrderTraversal(TreeNode root)
{
if (root == null) return;
Console.WriteLine(root.Value);
PreOrderTraversal(root.Left);
PreOrderTraversal(root.Right);
}
public static void PreOrderTraversal1(TreeNode root)
{
TreeNode current = root;
while (current != null)
{
Console.WriteLine(current.Value);
if (current.Left != null)
{
current = current.Left;
continue;
}
if (current.Right != null)
{
current = current.Right;
continue;
}
TreeNode parent = current.Parent;
while (parent.Right == current || parent.Right == null)
{
current = current.Parent;
parent = current.Parent;
if (parent == null) break;
}
if (current.Parent != null) current = current.Parent.Right; else break;
}
}
public static void PreOrderTraversal2(TreeNode root)
{
if (root == null)
{
return;
}
else
{
Console.WriteLine(root.Value);
if (root.Left != null)
{
root = root.Left;
}
else
{
if (root.Right != null)
{
root = root.Right;
}
else
{
TreeNode parent = root.Parent;
while (parent.Right == root || parent.Right == null)
{
root = root.Parent;
parent = root.Parent;
if (parent == null) break;
}
if (root.Parent != null) root = root.Parent.Right; else return;
}
}
PreOrderTraversal2(root);
}
}
static void Main(string[] args)
{
var tree =
new TreeNode(1,
new TreeNode(2,
new TreeNode(3,
new TreeNode(4, null, null),
new TreeNode(5, null, null)),
new TreeNode(6,
new TreeNode(7, null, null),
new TreeNode(8, null, null))),
new TreeNode(9,
new TreeNode(10,
new TreeNode(11, null, null),
new TreeNode(12, null, new TreeNode(13, null, null))),
new TreeNode(14,
new TreeNode(15, null, null),
new TreeNode(16, null, null))));
PreOrderTraversal2(tree);
}
}
}