Learn all these terms:
What is the maximum number of nodes in a binary tree of height
|
|
|
|
In a linked list, we store a reference to one node, all other nodes are reachable by following
In a binary tree, we store a reference to one node, all other nodes are reachable by following
static <E> void preorder(BinNode<E> rt) {
if (rt == null) { return; } // Empty subtree - do nothing
visit(rt); // Process root node
preorder(rt.left()); // Process all nodes in left
preorder(rt.right()); // Process all nodes in right
}
static <E> void inorder(BinNode<E> rt) {
if (rt == null) { return; } // Empty subtree - do nothing
inorder(rt.left()); // Process all nodes in left
visit(rt); // Process root node
inorder(rt.right()); // Process all nodes in right
}
static <E> void postorder(BinNode<E> rt) {
if (rt == null) { return; } // Empty subtree - do nothing
postorder(rt.left()); // Process all nodes in left
postorder(rt.right()); // Process all nodes in right
visit(rt); // Process root node
}
Trace the following traversal method…
public static <E> void mystery(BinNode<E> rt) {
Queue<BinNode<E>> queue = new LQueue<>();
queue.enqueue(rt);
while (queue.length() > 0) {
BinNode<E> cur = queue.dequeue();
visit(cur);
if (cur.left() != null) {
queue.enqueue(cur.left());
}
if (cur.right() != null) {
queue.enqueue(cur.right());
}
}
}
Space, Right Arrow or swipe left to move to next slide, click help below for more details