二叉树 广度优先遍历

  1. /**
  2. * 广度优先遍历
  3. * **/
  4. public void BreadthFirstTreeWalk(BSTreeNode<T> root, Action<BSTreeNode<T>> func) {
  5. if (root == null) {
  6. return;
  7. }
  8. List<BSTreeNode<T>> processQueue = new List<BSTreeNode<T>>();
  9. processQueue.Add(root);
  10. BSTreeNode<T> current;
  11. while (processQueue.Count != 0) {
  12. current = processQueue[0];
  13. processQueue.RemoveAt(0);
  14. func(current);
  15. if (current.left != null) {
  16. processQueue.Add(current.left);
  17. }
  18. if (current.right != null) {
  19. processQueue.Add(current.right);
  20. }
  21. }
  22. return;
  23. }





posted @ 2017-04-11 23:52  xiejunzhao  阅读(581)  评论(0编辑  收藏  举报