-
Notifications
You must be signed in to change notification settings - Fork 160
/
print-binary-tree.py
29 lines (26 loc) · 978 Bytes
/
print-binary-tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def printTree(self, root):
"""
:type root: TreeNode
:rtype: List[List[str]]
"""
def get_height(node):
return 0 if not node else 1 + max(get_height(node.left), get_height(node.right))
def update_output(node, row, left, right):
if not node:
return
mid = (left + right) // 2
self.output[row][mid] = str(node.val)
update_output(node.left, row + 1 , left, mid - 1)
update_output(node.right, row + 1 , mid + 1, right)
height = get_height(root)
width = 2 ** height - 1
self.output = [[''] * width for i in range(height)]
update_output(node=root, row=0, left=0, right=width - 1)
return self.output