1.
Convert Sorted Array to Binary Search Tree (Easy)
Given an integer array nums
where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right
2.
Sort List (Medium)
Given the head
of a linked list, return the list after sorting it in ascending order.
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next
3.
Construct Quad Tree (Medium)
Given a n * n
matrix grid
of 0's
and 1's
only, represent grid
with a Quad-Tree.
Return the root of the Quad-Tree representing grid
.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Each node has two attributes:
val
:True
if the node represents a grid of1's
orFalse
if the node represents a grid of0's
.isLeaf
:True
if the node is a leaf node on the tree orFalse
if the node has four children.
# Definition for a QuadTree node. class Node: def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight): self.val = val self.isLeaf = isLeaf self.topLeft = topLeft self.topRight = topRight self.bottomLeft = bottomLeft self.bottomRight = bottomRight
Construct a Quad-Tree from a two-dimensional area using the following steps:
- If the current grid has the same value (i.e all
1's
or all0's
), setisLeaf
True
and setval
to the value of the grid and set the four children toNull
and stop. - If the current grid has different values, set
isLeaf
toFalse
and setval
to any value and divide the current grid into four sub-grids. - Recurse for each of the children with the proper sub-grid.