对于一个简单二叉树 public class BTree { public BTree Left; public BTree Right; public int Value; } 左递归优先的枚举基本上就是 static IEnumerable<object> 枚举(BTree tree) { yield return tree.Value; if (tree.Left != null) { foreach (var v in 枚举(tree.Left)) yield return v; } if (tree.Right != null) { foreach (var v in 枚举(tree.Right)) yield return v; } }然后访问时可以写 foreach (var v in 枚举(tree)) { .......... }你那种混乱的代码,死抠底层而不会使用高效率的高层编程,实在不是c#风格。估计是学c语言把脑筋学得“死掉”了。
public class BTree
{
public BTree Left;
public BTree Right;
public int Value;
}
左递归优先的枚举基本上就是 static IEnumerable<object> 枚举(BTree tree)
{
yield return tree.Value;
if (tree.Left != null)
{
foreach (var v in 枚举(tree.Left))
yield return v;
}
if (tree.Right != null)
{
foreach (var v in 枚举(tree.Right))
yield return v;
}
}然后访问时可以写 foreach (var v in 枚举(tree))
{
..........
}你那种混乱的代码,死抠底层而不会使用高效率的高层编程,实在不是c#风格。估计是学c语言把脑筋学得“死掉”了。