Wins the Offer (XXII): Prints binary tree from the top down

Wins the Offer (XXII): Prints binary tree from the top down

Search micro-channel public number: 'AI-ming3526' or 'this small computer vision' for more algorithms, machine learning, dry
CSDN: https://blog.csdn.net/baidu_31657889/
GitHub: https://github.com/ aimi-cn / AILearners

First, the primer

This series is my brush "to prove safety Offer" brush off the cattle in question notes online, it aims to enhance the ability under its own algorithm.
View the complete algorithm to prove safety issues resolved Offer Click: to prove safety Offer complete analytical exercises

Second, the title

Downward from the print out of each node of the binary tree, with the layer node from left to right printing.

1, ideas

In fact, this topic is a hierarchical binary tree traversal problem.

Give an example:

file

As shown above, because the decision of the printing layer should be in order to print the root, so we begin to analyze the root of the tree. In order to print the next two child nodes of the node value of 8, we should save traversed node of the two nodes 10 to the value of 6 and a container, inside the container there are now two junctions Clicked. Printed from left to right as required, we first removed by nodes 6. After six print out the value of its values ​​were 5 and 7 of the two nodes into a data container. In this case there are three data container nodes, values ​​10,5 and 7, respectively. Next, we removed the node 10 from the data value of the container. Value is noted that the node 10 to node ratio of 5,7 to put in a container, this time off than to remove these two nodes, this is what we typically say FIFO, it is not difficult to see that this data container it should be a queue. Since the value of the node 5,7,9,11 are not child nodes, so long as printing can be sequentially.

So we can conclude that the method is printed in layers, each print when a node, if the node has a child node, a child node according to the node from left to right put into the queue at the end of a . Next, the first to remove the head of the queue node into the queue, repeating the previous printing operation, the queue until all nodes are printed so far.

2, programming

python

Code implementation:

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回从上到下每个节点值列表,例:[1,2,3]
    def PrintFromTopToBottom(self, root):
        # write code here
        if not root:
            return []
        else:
            currentStack1 = [root]
            res = []
            while currentStack1:
                nextStack = []
                for i in currentStack1:
                    if i.left : nextStack.append(i.left)
                    if i.right : nextStack.append(i.right)
                    res.append(i.val)
                currentStack1 = nextStack
            return res

AIMI-CN AI learning exchange group [1015286623] for more information on AI

Sharing technology, fun in life: our number of public computer vision this small push "AI" series News articles per week, welcome your interest!

Guess you like

Origin www.cnblogs.com/aimi-cn/p/11424767.html