LeetCode开心刷题五十二天——106. Construct Binary Tree from Inorder and Postorder Traversal107. Binary Tree Level Order Traversal II108. Convert Sorted Array to Binary Search Tree

时间:2019-11-11
本文章向大家介绍LeetCode开心刷题五十二天——106. Construct Binary Tree from Inorder and Postorder Traversal107. Binary Tree Level Order Traversal II108. Convert Sorted Array to Binary Search Tree,主要包括LeetCode开心刷题五十二天——106. Construct Binary Tree from Inorder and Postorder Traversal107. Binary Tree Level Order Traversal II108. Convert Sorted Array to Binary Search Tree使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
106. Construct Binary Tree from Inorder and Postorder Traversal
Medium

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

For example, given

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Return the following binary tree:

    3
   / \
  9  20
    /  \
   15   7

Thoughts:
totally similar with 105,but use 递归,低效
# 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 buildTree(self, inorder, postorder):
        """
        :type inorder: List[int]
        :type postorder: List[int]
        :rtype: TreeNode
        """
        if not inorder or not postorder:
            return None
        root=TreeNode(postorder[-1])
        index=inorder.index(postorder[-1])
        root.left=self.buildTree(inorder[:index],postorder[:index])
        root.right=self.buildTree(inorder[index+1:],postorder[index:-1])
        return root
        
        
        

107. Binary Tree Level Order Traversal II
Easy

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

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

这题和102,一丘之貉,虽说是求从底向上,但是二叉树的结构决定了没法直接从底下求,所以只能是先从上到下,再逆序或者每次结果插到最前面

就是简单的二叉树层次遍历。想要收集自底向上的遍历结果,只需要在自顶向下每次层次遍历完成时将此层的遍历结果插入到结果list的最前面就好了,即result_list.insert(0, level_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 levelOrderBottom(self,root):
        ans=[]
        if root == None:
            return ans
        q=[root]
        while len(q)!=0:
            ans.append(node.val for node in q)
            new_q=[]
            for node in q:
                if node.left:
                    new_q.append(node.left)
                if node.right:
                    new_q.append(node.right)
            q=new_q
        return reversed(ans)

108. Convert Sorted Array to Binary Search Tree
Easy

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

Given the sorted array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
     / \
   -3   9
   /   /
 -10  5

108也差不多是一个意思,都是把树分为左右两边,采用递归方法,简化问题。
# 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 sortedArrayToBST(self, nums):
        """
        :type nums: List[int]
        :rtype: TreeNode
        """
        if not nums:return None
        n=len(nums)
        mid=n//2
        root=TreeNode(nums[mid])
        root.left=self.sortedArrayToBST(nums[:mid])
        root.right=self.sortedArrayToBST(nums[mid+1:])
        return root
        
        

 

原文地址:https://www.cnblogs.com/Marigolci/p/11834289.html