1. 程式人生 > >102..103.199二叉樹的(鋸齒形)層次遍歷、右檢視(中等,樹)

102..103.199二叉樹的(鋸齒形)層次遍歷、右檢視(中等,樹)

102、給定一個二叉樹,返回其按層次遍歷的節點值(即逐層地,從左到右訪問所有節點)。

例如:
給定二叉樹: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其層次遍歷結果:

[
  [3],
  [9,20],
  [15,7]
]
# 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):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        result=[]
        currentnode=[]
        if not root:
            return result
        currentnode.append(root)
        while currentnode:
            single=[]
            l=len(currentnode)
            for i in range(l):
                current=currentnode.pop(0)
                if current.left:
                    currentnode.append(current.left)
                if current.right:
                    currentnode.append(current.right)
                single.append(current.val)
            result.append(single)
        return result

103、給定一個二叉樹,返回其節點值的鋸齒形層次遍歷。(即先從左往右,再從右往左進行下一層遍歷,以此類推,層與層之間交替進行)。

例如:
給定二叉樹 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回鋸齒形層次遍歷如下:

[
  [3],
  [20,9],
  [15,7]
]
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def zigzagLevelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        result=[]
        currentnode=[]
        if not root:
            return result
        currentnode.append(root)
        j=-1
        while currentnode:
            single=[]
            l=len(currentnode)
            for i in range(l):
                current=currentnode.pop(0)
                if current.left:
                    currentnode.append(current.left)
                if current.right:
                    currentnode.append(current.right)
                single.append(current.val)
            if j>0:
                single=single[::-1]
            j=-j
            result.append(single)
        return result

 

199、給定一棵二叉樹,想象自己站在它的右側,按照從頂部到底部的順序,返回從右側所能看到的節點值。

示例:

輸入: [1,2,3,null,5,null,4]
輸出: [1, 3, 4]
解釋:

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def rightSideView(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        result=[]
        currentnode=[root]
        while currentnode:
            single=[]
            l=len(currentnode)
            for i in range(l):
                current=currentnode.pop(0)
                if current.left:
                    currentnode.append(current.left)
                if current.right:
                    currentnode.append(current.right)
                single.append(current.val)
            result.append(single)
        return [s[-1] for s in result]

執行用時: 56 ms, 在Binary Tree Right Side View的Python3提交中擊敗了71.15%的使用者

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

class Solution:
    def rightSideView(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        ret = [root.val]
        left = self.rightSideView(root.left)
        right = self.rightSideView(root.right)
        ret  += right + left[len(right):]          
        return ret

第二個方法很簡單