File: root - text - article - 2020 - 02 - print-a-tree-level-by-level,-with-line-breaks.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 665 Views, 28551 Search Bots | 111 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 665 Views, 28551 Search Bots | 111 Words
| Browse | Archive
Hi, here's your problem today. This problem was recently asked by Apple:
You are given a tree, and your job is to print it level-by-level with linebreaks.
a
/ \
b c
/ \ / \
d e f g
The output should be
a
bc
defg
Here's a starting point:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 665 Views, 28551 Search Bots | 111 Words You are given a tree, and your job is to print it level-by-level with linebreaks.
a
/ \
b c
/ \ / \
d e f g
The output should be
a
bc
defg
Here's a starting point:
from collections import deque
class Node(object):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def __str__(self):
# Fill this in.
tree = Node('a')
tree.left = Node('b')
tree.right = Node('c')
tree.left.left = Node('d')
tree.left.right = Node('e')
tree.right.left = Node('f')
tree.right.right = Node('g')
print tree
# a
# bc
# defg
Related Articles
- Algorithm Interview: Smallest Number that is not a Sum of a Subset of List
- Daily Interview Problem: Maximum Profit From Stocks
- Reverse a Directed Graph
- Algorithm Interview Question: Symmetric k-ary Tree
- Sorting a list with 3 unique numbers
- Absolute Path
- Daily Interview Puzzle: Intersection of Linked Lists
- New Vulnerabilities (CVE-2016-4581) have been detected in CentOS/RHEL/CloudLinux 7
- Compare Version Numbers
- Daily Interview Problem: Buddy Strings
©2006~2024 SteakOverCooked - 0.01295 Seconds(s) - 1567.584 KB/s - 35 Online Memory: 492.75 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" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !