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 递归,低效
首先要判空
其次,inorder postorder的下标方面,区别在,中序和后序想要空过的根节点位置不同,导致坐标不同
# 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
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)
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
不知道为啥,但这句话很关键,而且只能这样判断
if not nums:return None
或者这样
if nums==[]:
return None
ps:nums[0:0]==[ ]
Given a singly linked list 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 linked list: [-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
原来很怕树类的题,但也不知是最近的题目比较集中的反应了这里的知识还是,其实树的问题基本都是套路
109和108将List转成Array就完全一样,并且比快慢指针的效率还高。但是为了锻炼下双指针的写法,还是舍近求远下,
快慢指针的核心思想是:两个指针,一个一次一步,一个两步,这样快的走到终点时,慢的刚好再中间。如果存在值的话,中间就是节点要赋值的地方
把mid之前的划到一起,之后的一起,分而治之。注意:以前是list时,可以用下标遍历,改为链表后可用next
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None # 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 sortedListToBST(self, head): """ :type head: ListNode :rtype: TreeNode """ return self.helper(head,None) def helper(self,head,tail): if head==tail: return None if head.next==tail: return TreeNode(head.val) mid,quick=head,head while quick!=tail and quick.next!=tail: quick=quick.next.next mid=mid.next root=TreeNode(mid.val) root.left=self.helper(head,mid) root.right=self.helper(mid.next,tail) return root
浙公网安备 33010602011771号