Xây dựng cây nhị phân
Hướng dẫn chi tiết và cách triển khai Python cho bài toán 'Xây dựng cây nhị phân'.
1. Tìm hiểu
Bài toán 'Xây dựng cây nhị phân' là một thách thức chính trong phần Cây.
Việc triển khai này tập trung vào logic cấp trung bình trong Python.
Chúng tôi ưu tiên độ chính xác về mặt kỹ thuật và khả năng đọc mã trong các giải pháp được cung cấp của chúng tôi.
2. Real-World Applications
3. Visual Intuition
Trực quan hóa luồng logic cho Xây dựng cây nhị phân.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Đọc kỹ báo cáo vấn đề về Xây dựng cây nhị phân.
2. Formulate brute force
Soạn thảo một giải pháp lặp lại đơn giản.
3. Identify inefficiency
Tìm các phép tính dư thừa.
4. Optimize search path
Sử dụng hàm băm hoặc sắp xếp để tăng tốc quá trình.
5. Final Implementation
Làm sạch mã cho tiêu chuẩn sản xuất.
Tuyên bố vấn đề
Cho hai mảng số nguyên thứ tự trước và thứ tự trong đó thứ tự trước là lần duyệt thứ tự trước của cây nhị phân và thứ tự thứ tự là lần duyệt thứ tự của cùng một cây, xây dựng và trả về cây nhị phân.
Cây phải được trả về dưới dạng danh sách thứ tự cấp độ. Triển khai hàm buildTree(preorder: list, inorder: list) -> list.
- •1 <= preorder.length <= 3000
- •inorder.length == preorder.length
- •-3000 <= preorder[i], inorder[i] <= 3000
- •preorder and inorder consist of unique values
- •Each value of inorder also appears in preorder
- •preorder is guaranteed to be the preorder traversal of the tree
- •inorder is guaranteed to be the inorder traversal of the tree
Ví dụ
[3,9,20,15,7], [9,3,15,20,7]
[3,9,20,None,None,15,7]
Preorder: root is 3. In inorder, 9 is to the left of 3 (left subtree) and [15,20,7] is to the right (right subtree). Recursively build: left subtree is just [9], right subtree has root 20 with children 15 and 7.
[-1], [-1]
[-1]
Single node tree.
Need a Hint?
Edge Cases to Watch
- Cấu trúc đầu vào trống
- Đầu vào phần tử đơn
- Giới hạn số lớn
Sẵn sàng để giải quyết?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Thông tin chi tiết và biến thể của cuộc phỏng vấn
Phân tích độ phức tạp
Tại sao thời gian: Directly evaluates all possibilities.
Tại sao không gian: Uses standard local memory.
Tại sao thời gian: Optimized paths reduce total operations.
Tại sao không gian: May trade memory for speed.
Mã Python giải pháp tối ưu hóa
Mã Python giải pháp tối ưu hóa
def build_tree_opt(preorder, inorder):
if isinstance(preorder, list):
r = build_tree_opt_helper(preorder, inorder)
return tree_to_list(r)
return build_tree_opt_helper(preorder, inorder)
def build_tree_opt_helper(preorder: list, inorder: list) -> TreeNode:
if not preorder or not inorder:
return None
root_val = preorder[0]
root = TreeNode(root_val)
mid = inorder.index(root_val)
root.left = build_tree_opt_helper(preorder[1:mid+1], inorder[:mid])
root.right = build_tree_opt_helper(preorder[mid+1:], inorder[mid+1:])
return rootMã Brute Force (Bảo vệ spoiler)
Mã Brute Force (Bảo vệ spoiler)
def build_tree_brute(preorder, inorder):
if isinstance(preorder, list):
r = build_tree_brute_helper(preorder, inorder)
return tree_to_list(r)
return build_tree_brute_helper(preorder, inorder)
def build_tree_brute_helper(preorder: list, inorder: list) -> TreeNode:
if not preorder or not inorder: return None
root = TreeNode(preorder[0])
mid = inorder.index(preorder[0])
root.left = build_tree_brute_helper(preorder[1:mid+1], inorder[:mid])
root.right = build_tree_brute_helper(preorder[mid+1:], inorder[mid+1:])
return rootAlgorithm Pattern Checklist
When dealing with Trees data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
Key Revision Notes
Áp dụng các thuộc tính bài toán của Cây Tiêu chuẩn.
Câu hỏi liên quan
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
Tài nguyên Python được đề xuất
Mở rộng kiến thức của bạn với các hướng dẫn tương tác, bảng ghi chú và so sánh mã có liên quan.
Vòng lặp Python
Tìm hiểu cách sử dụng vòng lặp Python để lặp lại dữ liệu. Nắm vững các phương pháp hay nhất về vòng lặp for, while, ngắt, tiếp tục và lặp bằng các ví dụ tương tác.
Cách sắp xếp danh sách trong Python
Tìm hiểu cách sắp xếp danh sách trong Python bằng phương thức Sort() và hàm Sort(). Khám phá các ví dụ về sắp xếp khóa tùy chỉnh và thứ tự đảo ngược.
Bảng cheat phương thức chuỗi Python
Hướng dẫn tham khảo đầy đủ về thao tác chuỗi Python. Làm chủ định dạng, tìm kiếm, phân tách, thay thế và kiểm tra thuộc tính chuỗi.
Python vs JavaScript: Ngôn ngữ lập trình nào tốt nhất?
So sánh toàn diện giữa Python và JavaScript. Khám phá sự khác biệt về cú pháp, hiệu suất, trường hợp sử dụng (phụ trợ so với giao diện người dùng) và các ví dụ về mã hóa.