数据结构与算法 —— 二叉树 01

上节介绍了二叉树的一些基本概念,这篇文章开始,我们开始学习二叉树的一些算法问题,今天先看一些层次遍历的题目。

 二叉树的层次遍历

题目

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / 
  9  20
    /  
   15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

解题思路

维护一个list,list里面是当前层次的节点的集合。然后循环list,把下个层次的节点再加入到list中去,直到list为空。

# 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 root is None:
            return res
        q = []
        q.append(root)
        while len(q) != 0:
            temp = []
            length = len(q)
            for i in range(length):
                r = q.pop(0)
                if r.left:
                    q.append(r.left)
                if r.right:
                    q.append(r.right)
                temp.append(r.val)
            res.append(temp)
        return res
                

 二叉树的层次遍历II

题目

给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

例如:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / 
  9  20
    /  
   15   7

返回其自底向上的层次遍历为:

[
  [15,7],
  [9,20],
  [3]
]

解题思路

和上道题的思路是一致的,只是在生成结果的时候把数据插入到最前面,用到list的insert方法。

# 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 levelOrderBottom(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        res = []
        if not root:
            return res
        q = []
        q.append(root)
        while len(q) > 0:
            temp =[]
            length = len(q)
            for i in range(length):
                node = q.pop(0)
                if node.left:
                    q.append(node.left)
                if node.right:
                    q.append(node.right)
                temp.append(node.val)
            res.insert(0,temp)
        return res
    

  

二叉树的最小深度

题目

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

    3
   / 
  9  20
    /  
   15   7

返回它的最小深度  2.

解题思路

和前面的解题思路基本一致,就是要再维护一个int的deep记录最小的深度,当左右节点都为空的时间,直接就可以返回了。

# 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 minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        q=[]
        q.append(root)
        deep = 0
        while len(q)!=0:
            deep +=1
            length = len(q)
            for i in range(length):
                node = q.pop(0)
                if not (node.left or node.right):
                    return deep
                else:
                    if node.left:
                        q.append(node.left)
                    if node.right:
                        q.append(node.right)

  

总结一波:对于二叉树的层次遍历,只要一直维护当前层次的node的list就好了。感觉最重要的事情就是这样。

coding交流群:226704167,愿和各位一起进步!

微信公众号:

原文地址:https://www.cnblogs.com/lip0121/p/9116537.html