当前位置:网站首页>Optimization of deep recursion
Optimization of deep recursion
2022-07-22 12:48:00 【Lava of Rodinia】
Optimization of deep recursion
using System.Collections;
Stack st = new Stack();
// Order traversal in binary tree
// recursive ....
List<int> list = new List<int>();
List<int> inorderTraversal(TreeNode root)
{
if (root == null)
return list;
inorder(root.left);
list.Add(root.val);
inorder(root.right);
return list;
}
void inorder(TreeNode root)
{
if (root == null)
return;
inorder(root.left);
list.Add(root.val);
inorder(root.right);
}
foreach (var item in inorderTraversal(new TreeNode(1, null, new TreeNode(2, new TreeNode(3, null, null), null))))
{
Console.WriteLine(item);
}
// Explicit stack :
List<int> inorderTraversal1(TreeNode root)
{
List<int> res = new List<int>();
LinkedList<TreeNode> stk = new LinkedList<TreeNode>();
while (root != null || !(stk.Count == 0))
{
while (root != null)
{
stk.AddLast(root);
root = root.left;
}
root = stk.Last();
stk.RemoveLast();
res.Add(root.val);
root = root.right;
}
return res;
}
Console.WriteLine("======================");
foreach (var item in inorderTraversal1(new TreeNode(1, null, new TreeNode(2, new TreeNode(3, null, null), null))))
{
Console.WriteLine(item);
}
// Fibonacci
int Fib(int n)
{
if (n == 1 || n == 2) return 1;
else return Fib(n - 1) + Fib(n - 2);
}
Console.WriteLine("=========== Fibonacci ===========");
Console.WriteLine(Fib(16));
// Use the cache
int FibCache(int n)
{
int[] cache = {
0, 1, 1 };
int _fib(int n)
{
if (cache[n] == 1) return cache[n];
cache[n] = _fib(n - 1) + _fib(n - 2);
return cache[n];
}
return _fib(n);
}
Console.WriteLine("=========== Fibonacci 2===========");
Console.WriteLine(Fib(16));
public class TreeNode
{
public int val {
get; set; }
public TreeNode left;
public TreeNode right;
public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
{
this.val = val;
this.left = left;
this.right = right;
}
}
result :
1
3
2
======================
1
3
2
=========== Fibonacci ===========
987
=========== Fibonacci 2===========
987
D:\Csharp\ConsoleTest1\ConsoleTest1\bin\Debug\net6.0\ConsoleTest1.exe ( process 2356) Exited , The code is 0.
To automatically close the console when debugging stops , Please enable “ Tools ”->“ Options ”->“ debugging ”->“ Automatically close the console when debugging stops ”.
Press any key to close this window . . .
边栏推荐
- The basic knowledge of K-line diagram occupies an important position
- Spark Learning Notes (I) -- simulating distributed computing
- Shell learning notes (VI) -- practical battle I: Script Writing
- QGraphicsView图形视图框架使用(六)图元动画
- pip常用命令
- 【Golang | gRPC】使用gRPC实现Watch功能
- 安全体系建设-安全设备监控具体操作流程
- LeGo-LOAM 跑通与源码学习
- IT互联网行业在数据安全和网络安全实践落地案例 脱敏版
- EasyCVR平台安全扫描提示Go pprof调试信息泄露的解决办法
猜你喜欢
The cloud XR platform supports the rapid landing of immersive experience applications
C语言指针详解
k线图基本知识占据里面重要的位置
Detailed explanation of C language pointer
虚拟DOM是什么
[golang | grpc] use grpc to realize the watch function
哈根达斯召回香草味冰淇淋,广州多个商超下架相关产品
Serial port based SD_ Card system
The basic knowledge of K-line diagram occupies an important position
无代码生产新模式探索
随机推荐
红队具体怎么进行测试工作开展的
121. 买卖股票的最佳时机
Homework in the second week
pip常用命令
Serial port based SD_ Card system
JS console alert repl error variable variable declaration promotion data type typeof
AD常用术语
Three high notes on Architecture Design 2.0 methodology and practice of large distributed system architecture
The cloud XR platform supports the rapid landing of immersive experience applications
SIM卡交换方案的工作原理
Easycvr platform security scanning prompt go pprof debugging information leakage solution
AD常用快捷键
Misunderstanding of data center construction
k线图基本知识占据里面重要的位置
数据安全落地方案举例让你的数据安全之路少走弯路
Aike AI frontier promotion (7.21)
ECCV 2022 | interpret depth forgery detection by analyzing image matching
A-LOAM源码阅读
防患于未然是最好的解决勒索软件方法
A-loam source code reading