首页 > 代码库 > 二叉树 广度优先遍历

二叉树 广度优先遍历

  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. }



null


二叉树 广度优先遍历