To the Top
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 | 342 Views, 21345 Search Bots | 111 Words

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


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

Related Articles

  1. Binary Tree Level with Minimum Sum
  2. Batch Programming in XP
  3. Daily Interview Problem: Deepest Node in a Binary Tree
  4. Daily Interview Puzzle: Intersection of Linked Lists
  5. ImageMagick Vulnerabilities -  CVE-2016–3714
  6. Find the non-duplicate number
  7. Daily Interview Problem: Tree Serialization
  8. Daily Interview Problem: Reverse Integer
  9. [Daily Problem] Validate Balanced Parentheses
  10. Algorithm Interview: Shifted String

Comments (0)

Your Email (Domain Part Not Exposed):

Your Comments:

Privately By Mail Colors More Smileys S x y @

Verification (Click Image 2 Refresh):

    Be the first one to comment this page !


Page Edited: October 30 2020 14:21:09 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />