Python implements printing of all nodes in a layer of a binary tree

Source: Internet
Author: User

Not much to say, direct patch, as shown below

#-*-coding:utf-8-*-#Defining a binary tree node classclassTreeNode (object):def __init__(self,data=0,left=0,right=0): Self.data=Data Self.left=Left Self.right= Right#traverse all nodes of a layer and printdefTranslevel (root,level):ifRoot = =None:return    Else:        ifLevel = = 1 andisinstance (Root, TreeNode):Print "%s"%Root.data,Else:            ifisinstance (Root, TreeNode): Translevel (Root.left, level-1) Translevel (root.right, level-1)#established two-fork tree# ------------------------#Root#7 8#6#2 5#1 3 4## -------------------------N1= TreeNode (data=1) N2= TreeNode (2, n1,0) N3= TreeNode (3) N4= TreeNode (4) N5= TreeNode (5, N3,n4) N6= TreeNode (6, n2,n5) N7= TreeNode (7, n6,0) N8= TreeNode (8) Root= TreeNode ('Root', N7,N8) Translevel (Root,5)

Resources:

1. Implementation of binary tree traversal of Python data structure
Http://www.cnblogs.com/yupeng/p/3414451.html

2. Print the nodes of a certain layer of the binary tree
Http://www.cnblogs.com/-Lei/archive/2013/02/25/2928629.html

Python implements printing of all nodes in a layer of a binary tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.