To the Top
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 | 334 Views, 21787 Search Bots | 104 Words

Subscribe to Feed Burner | Browse | Archive
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:


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
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 334 Views, 21787 Search Bots | 104 Words Subscribe to Feed Burner

Related Articles

  1. ImageMagick Vulnerabilities -  CVE-2016–3714
  2. Daily Interview Problem: Largest Product of 3 Elements I
  3. Daily Interview Problem: Spiral Traversal of Grid
  4. Daily Interview Problem: Find the k-th Largest Element in a List
  5. Algorithm Interview: Determine If Linked List is Palindrome
  6. Daily Interview Problem: Merge Overlapping Intervals
  7. Skip the readings, focus on problems. And use all the hints!
  8. Sort a Partially Sorted List
  9. Algorithm Interview Question: Find the Single Element in an Array of Duplicates
  10. Detect Linked List Cycle

Comments (0)

    Be the first one to comment this page !


Page Edited: May 11 2024 14:36:49 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />