[Leetcode Series] [] [medium] algorithm binary tree traversal sequence (stack, DFS)

topic:

Topic links:  https://leetcode-cn.com/problems/binary-tree-level-order-traversal/

 

Problem-solving ideas:

Method a: Stack (time O (N), the space O (N))

variable:

  1. stack: the stack when traversal, initialization, only the root node into
  2. res: the result set
  3. val_lst: The current need to be added to the result set list

Process:

Stack initialization, the root node 3 into the stack, and then traversing:

Get the first element stack, put into val_lst in this case was found and the left and right subtrees 3 put into the stack

And at this point has been traversed in the traversal start is all the elements in the stack, out of this traverse, the value val_lst is placed in the res:

Continuing traversing, get the first element in the stack 2, the 2 val_lst and put into the stack, then the left and right subtrees of the stack 2:

9 will continue to val_lst placed in the stack 9, 9 because there is no left and right subtrees, elements in the stack does not need to add

At this point this has been traversed all the elements needed to traverse the exit in the traversal, the val_lst added to the result set res, the next time to continue to traverse:

Continue the walk until the stack is empty

 

Method two: DFS (time O (N), the space O (N))

DFS searches, add a level tagging, identifying the current value of the result where necessary to add a layer of

 

Code:

method one:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []
        
        stack = [root]
        res = []
        while stack:
            val_lst = []
            for index in range(0, len(stack)):
                node = stack[0]
                stack.pop(0)
                val_lst.append(node.val)
                if node.left:
                    stack.append(node.left)
                    
                if node.right:
                    stack.append(node.right)
                    
            res.append(val_lst)
            
        return res
                
                

 

Method Two:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        def create_res(res, node, level):
            print(res)
            if not node:
                return res
            
            if len(res) == level:
                res.append([])
                
            res[level].append(node.val)
            if node.left:
                create_res(res, node.left, level + 1)
                
            if node.right:
                create_res(res, node.right, level + 1)
                
            return res
        
        return create_res([], root, 0)
                
        

 

Published 100 original articles · won praise 4 · Views 1459

Guess you like

Origin blog.csdn.net/songyuwen0808/article/details/105399162