/**
* 广度优先遍历
* **/
public void BreadthFirstTreeWalk(BSTreeNode<T> root, Action<BSTreeNode<T>> func) {
if (root == null) {
return;
}
List<BSTreeNode<T>> processQueue = new List<BSTreeNode<T>>();
processQueue.Add(root);
BSTreeNode<T> current;
while (processQueue.Count != 0) {
current = processQueue[0];
processQueue.RemoveAt(0);
func(current);
if (current.left != null) {
processQueue.Add(current.left);
}
if (current.right != null) {
processQueue.Add(current.right);
}
}
return;
}
时间: 2024-10-17 02:06:50