Specify the correct implementation of in-order traverse algorithm for binary trees.
4 phương ánCó giải thích
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "
void inOrder(Node p) {
if (p != null) {
inOrder(p.left);
visit(p);
inOrder(p.right);
}
}". Duyệt giữa cần đúng hai điều: trình tự con trái, thăm gốc, con phải; và điều kiện dừng khi gặp cây rỗng, vì mỗi lá vẫn gọi tiếp vào hai liên kết null. Bản này kiểm p khác null trước rồi mới đi xuống nên đệ quy chắc chắn kết thúc. Phương án "
void inOrder(Node p) {
inOrder(p.left);
visit(p);
inOrder(p.right);
}" không có điểm dừng nào, lời gọi đầu tiên đã lao xuống bên trái mãi và sinh lỗi truy cập null hoặc tràn ngăn xếp. Phương án "
void inOrder(Node p) {
if (p == null) {
inOrder(p.left);
visit(p);
inOrder(p.right);
}
}" đảo ngược điều kiện: với cây không rỗng nó chẳng làm gì, còn khi p là null nó lại đọc p.left nên lỗi ngay. Phương án "
void inOrder(Node p) {
if (p != null) {
visit(p);
inOrder(p.left);
inOrder(p.right);
}
}" dừng đúng nhưng thăm gốc trước hai con, đó là duyệt trước, và trên cây tìm kiếm nhị phân nó không cho dãy tăng dần.
Cùng phần 4
What is the minimum number of nodes in a nearly complete binary tree with height 4?
(In a tree the height of root is 1, and a binary tree with height h is called nearly complete if the tree is complete at level d for d = 1, 2, ..., h-1, and the leaves in the last level are all in the leftmost positions).Có lời giảiCâu hỏiTo implement an AVL tree, a concept balance factor is introduced (bal = height(right)-height(left)). Suppose an AVL tree is created by inserting to the tree the following keys sequentially:
6, 3, 7, 2, 5, 1
What is the balance factor of the node 6? (please note that the tree is still AVL)Có lời giảiCâu hỏiSuppose the binary search tree T is made by inserting the following keys consequently: 6, 7, 8, 9, 3, 2, 5, 4
What is the result of breadth-first traverse of T?Có lời giảiCâu hỏiCả 76 câu của chủ đề76 câu
Xuất hiện trong 1 bộ đề
Đi tiếp từ đây