File: root - text - article - 2020 - 02 - making-a-height-balanced-binary-search-tree.txt.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 594 Views, 26277 Search Bots | 104 Words
| Browse | Archive
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 594 Views, 26277 Search Bots | 104 Words
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Hi, here's your problem today. This problem was recently asked by Google:
Given a sorted list, create a height balanced binary search tree, meaning the height differences of each node can only differ by at most 1.
Here's some code to start with:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 594 Views, 26277 Search Bots | 104 Words Given a sorted list, create a height balanced binary search tree, meaning the height differences of each node can only differ by at most 1.
Here's some code to start with:
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def __str__(self):
answer = str(self.value)
if self.left:
answer += str(self.left)
if self.right:
answer += str(self.right)
return answer
def create_height_balanced_bst(nums):
# Fill this in.
tree = create_height_balanced_bst([1, 2, 3, 4, 5, 6, 7, 8])
# 53214768
# (pre-order traversal)
# 5
# / \
# 3 7
# / \ / \
# 2 4 6 8
# /
# 1
![Subscribe to Feed Burner Subscribe to Feed Burner](/images/form/rss.gif)
Related Articles
- Generate All IP Addresses
- [Daily Problem] Angles of a Clock
- Algorithm Interview Question: Symmetric k-ary Tree
- [Daily Problem] Remove Consecutive Nodes that Sum to 0
- [Daily Problem] Course Prerequisites
- Spreadsheet Columns
- Algorithm Interview: Maximum Path Sum in Binary Tree
- Daily Interview Question: Find Cycles in a Graph
- Spectrum Master
- Daily Interview Puzzle: Falling Dominoes
©2006~2024 SteakOverCooked - 0.01104 Seconds(s) - 4919.774 KB/s - 48 Online
Memory: 517.04 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
![](/images/top.jpg)
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !