The sword refers to offer 22. Print binary tree (binary tree) from top down

Source: Internet
Author: User

Title Description

Each node of the two-fork tree is printed from top to bottom, and the same-level node prints from left to right.

Title Address

https://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701?tpId=13&tqId=11175&rp=2&ru=/ta/ Coding-interviews&qru=/ta/coding-interviews/question-ranking

Ideas

Using queues

Because the order in which the layers are printed determines that the root node should be printed first, we start by analyzing the root node of the tree at 8. In order to be able to print two sub-nodes with a value of 8, we should traverse the node to save two nodes with a value of 6 and 10 in a container, and now there are two nodes in the container. According to the requirement of printing from left to right, we first take out the node of 6. After printing out the value 6, place the two nodes of the value 5 and 7 respectively into the data container. At this point there are three nodes in the data container, with values of 10, 5, and 7. Next we take the node with a value of 10 from the container, notice that the node with a value of 10 is the 5,7 node first into the container, then the two nodes are taken out first, while the 9 and 11 are placed into the data container, which is what we usually say first-in, first-out, so the data container is a queue. Because nodes with a value of 5,7,9,11 have no sub-nodes, they can be printed in order.

By example, we find the rule of printing binary trees from top to bottom: each time a node is printed, if the node has sub-nodes, then the left and right nodes of the node are placed at the end of a queue in order. Next to the head of the queue, take the first node that goes into the queue and repeat the previous print operation until all the nodes in the queue are printed.

Python

#-*-coding:utf-8-*-classTreeNode:def __init__(self, x): Self.val=x self.left=None self.right=Nonenode1= TreeNode (8) Node2= TreeNode (6) Node3= TreeNode (10) Node4= TreeNode (5) Node5= TreeNode (7) Node6= TreeNode (9) Node7= TreeNode (11) Node1.left=Node2node1.right=Node3node2.left=Node4node2.right=Node5node3.left=Node6node3.right=Node7classSolution:#returns a list of values from top to bottom for each node, for example: [All-in-one]    defPrintfromtoptobottom (self, root):#Write code hereAns = []        if  notRoot:returnans Queue=[] queue.append (Root) whileQueue:cur=queue.pop (0) ans.append (cur.val)ifcur.left:queue.append (cur.left)ifcur.right:queue.append (cur.right)returnansif __name__=='__main__': Result=solution (). Printfromtoptobottom (Node1)Print(Result)

The sword refers to offer 22. Print binary tree (binary tree) from top down

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.