zoukankan      html  css  js  c++  java
  • 栈和队列_leetcode102

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

    class Solution(object):
    def levelOrder(self, root):
    """
    :type root: TreeNode
    :rtype: List[List[int]]
    """

    res = []

    if not root :
    return res

    queue = []

    queue.append(root)

    while queue:
    node = queue.pop()

    res.append(node.val)

    if node.left:
    queue.append(node.left)
    if node.right:
    queue.append(node.right)

    return res





    class Solution2(object):
    def levelOrder(self, root):
    """
    :type root: TreeNode
    :rtype: List[List[int]]
    """

    res = []

    if not root :
    return res

    queue = []

    level = 0
    pair = (root,level)
    queue.append(pair)

    while queue:
    pair = queue.pop()

    if len(res) == pair[1]:
    res.append([])
    res[pair[1]].append(pair[0].val)

    if pair[0].right:
    queue.append((pair[0].right,pair[1]+1))
    if pair[0].left:
    queue.append((pair[0].left,pair[1]+1))



    return res



    # 队列中新进的节点都是同一层的节点
    class Solution3(object):
    def levelOrder(self, root):
    """
    :type root: TreeNode
    :rtype: List[List[int]]
    """

    res = []

    if not root :
    return res

    queue = []
    queue.append(root)

    while queue:
    level = []

    for i in range(len(queue)): # 这段代码写的不好 20100304
    node = queue.pop(0)
    level.append(node.val)

    if node.left:
    queue.append(node.left)
    if node.right:
    queue.append(node.right)

    res.append(level)
    return res

    # 修改版
    class Solution32(object):
    def levelOrder(self, root):
    """
    :type root: TreeNode
    :rtype: List[List[int]]
    """

    res = []

    if not root :
    return res

    queue = []
    queue.append(root)

    while queue:

    level = []
    length = len(queue)
    for i in range(length): # 这样改的就比较清楚了
    node = queue.pop(0)
    level.append(node.val)

    if node.left:
    queue.append(node.left)
    if node.right:
    queue.append(node.right)

    res.append(level)
    return res

    class Solution4(object):
    def levelOrder(self, root):
    """
    :type root: TreeNode
    :rtype: List[List[int]]
    """

    res = []

    self.level(root,0,res)

    return res

    def level(self,node,lev,res):
    if not node:
    return

    if len(res) == lev:
    res.append([])

    res[lev].append(node.val)

    self.level(node.left,lev+1,res)
    self.level(node.right,lev+1,res)
  • 相关阅读:
    leetcode第35题--Valid Sudoku
    leetcode 34 Search Insert Position
    leetcode第33题--Search for a Range
    leetcode第32题--Search in Rotated Sorted Array
    leetcode第31题--Longest Valid Parentheses
    leetcode第30题--Next Permutation
    leetcode第29题--Substring with Concatenation of All Words
    leetcode第28题--Divide Two Integers
    leetcode第27题--Implement strStr()
    17_7_7 JDBC存储过程 + 事务
  • 原文地址:https://www.cnblogs.com/lux-ace/p/10547334.html
Copyright © 2011-2022 走看看