二叉树的修改与构造
修改与构造题目1:106.从中序与后序遍历序列构造二叉树
【题目】
给定两个整数数组 `inorder` 和 `postorder` ,
其中 `inorder` 是二叉树的中序遍历,
`postorder` 是同一棵树的后序遍历,
请你构造并返回这颗 二叉树。
【解答】
//记录的inorder 数据-坐标i的映射
Map<Integer,Integer> map = new HashMap();
public TreeNode buildTree(int[] inorder, int[] postorder) {
//参考:https://www.bilibili.com/video/BV1Q3411T7P6/?spm_id_from=333.337.search-card.all.click&vd_source=77af280ef141120c4835d70407ae029a
int n = inorder.length;
for(int i=0;i<n;i++){
map.put(inorder[i],i);
}
return build(inorder,postorder,0,n-1,0,n-1);
}
public TreeNode build(int[] inorder,int[] postorder,int il,int ir,int pl,int pr){
if(il>ir || pl>pr){
return null;
}
//当前根节点在中序遍历的位置inorderIndex,在后续遍历list的位置为pr
int inorderIndex= map.get(postorder[pr]);
//当前节点作为根节点,其左子树的节点数=中序数组的根节点位置inorderIndex-il
int k = inorderIndex-il;
TreeNode root = new TreeNode(postorder[pr]);
//
root.left = build(inorder,postorder,il,inorderIndex-1,pl,pl+k-1);
root.right = build(inorder,postorder,inorderIndex+1,ir,pl+k,pr-1);
return root;
}
修改与构造题目2:105.从前序与中序遍历序列构造二叉树
【题目】
给定两个整数数组 `preorder` 和 `inorder` ,
其中 `preorder` 是二叉树的**先序遍历**,
`inorder` 是同一棵树的**中序遍历**,
请构造二叉树并返回其根节点。
【解答】
Map<Integer,Integer> map = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
int n = inorder.length;
for(int i=0;i<n;i++){
map.put(inorder[i],i);
}
return build(preorder,0,n-1,0,n-1);
}
public TreeNode build(int[] preorder,int pl,int pr, int il,int ir){
if(il>ir || pl>pr){
return null;
}
//当前的根节点数据:preorder[pl]
TreeNode root = new TreeNode(preorder[pl]);
int inorderRootIndex = map.get(preorder[pl]);
//左子树的数量
int k = inorderRootIndex-il;
root.left=build(preorder, pl+1,pl+k, il, inorderRootIndex-1);
root.right=build(preorder, pl+k+1, pr, inorderRootIndex+1, ir);
return root;
}
修改与构造题目3:226.翻转二叉树
【题目】
给你一棵二叉树的根节点 `root` ,翻转这棵二叉树,并返回其根节点。
【解答】
public TreeNode invertTree(TreeNode root) {
if(null==root){
return root;
}
TreeNode left = root.left;
TreeNode right = root.right;
root.left = right;
root.right = left;
invertTree(root.left);
invertTree(root.right);
return root;
}
修改与构造题目4:654.最大二叉树
【题目】
给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:
- 二叉树的根是数组中的最大元素。
- 左子树是通过数组中最大值左边部分构造出的最大二叉树。
- 右子树是通过数组中最大值右边部分构造出的最大二叉树。
通过给定的数组构建最大二叉树,并且输出这个树的根节点。
【解答】
public TreeNode constructMaximumBinaryTree(int[] nums) {
return constructMaximumBinaryTree(nums,0,nums.length-1);
}
public TreeNode constructMaximumBinaryTree(int[] nums,int left,int right){
if(left>right){
return null;
}
int max = nums[left];
int index =left;
for(int i=left+1;i<=right;i++){
if(nums[i]>max){
max = nums[i];
index = i;
}
}
TreeNode node = new TreeNode(max);
node.left=constructMaximumBinaryTree(nums,left,index-1);
node.right=constructMaximumBinaryTree(nums,index+1,right);
return node;
}
二叉搜索树的属性
二叉搜索树的属性题目1:700.二叉搜索树中的搜索
【题目】
给定二叉搜索树(BST)的根节点 `root` 和一个整数值 `val`。
你需要在 BST 中找到节点值等于 `val` 的节点。
返回以该节点为根的子树。 如果节点不存在,则返回 `null` 。
【解答】
public TreeNode searchBST(TreeNode root, int val) {
if(null==root){
return null;
}
if(val==root.val){
return root;
}else if(val>root.val){
return searchBST(root.right,val);
}else{
return searchBST(root.left,val);
}
}
二叉树公共祖先问题
公共祖先问题1:236.二叉树的最近公共祖先
【题目】
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
【解答】
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(null==root){
return null;
}
//题目的前提:p != q
// p 和 q 均存在于给定的二叉树中。
if(p==root || q==root){
return root;
}
/*
1
2 3
4 5 6 7
*/
TreeNode left = lowestCommonAncestor(root.left,p,q);
TreeNode right = lowestCommonAncestor(root.right,p,q);
//假设找4,5的最近公共祖先
//站在root=1的角度:left =2,right=null,返回root=2
//站在root=2的角度,left=4,right=5,返回root=2
if(null!=left && null!=right){
return root;
}else if(null!=left && null==right){
return left;
}else if(null==left && null!=right){
return right;
}else{
return null;
}
}
公共祖先问题2:235.二叉搜索树的最近公共祖先
【题目】
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
【解答】
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(null==root){
return null;
}
if(root==p || root==q){
return root;
}
TreeNode left = null;
TreeNode right =null;
if(p.val<root.val || q.val<root.val){
left = lowestCommonAncestor(root.left,p,q);
}
if(p.val>root.val || q.val>root.val){
right = lowestCommonAncestor(root.right,p,q);
}
if(null!=left && null!=right){
return root;
}else if(null!=left && null==right){
return left;
}else if(null==left && null!=right){
return right;
}else{
return null;
}
}
二叉搜索树的修改与构造
二叉搜索树修改与构造题1:701.二叉搜索树的插入操作
【题目】
给定二叉搜索树(BST)的根节点 `root` 和要插入树中的值 `value` ,将值插入二叉搜索树。
返回插入后二叉搜索树的根节点。 输入数据保证,新值和原始二叉搜索树中的任意节点值都不同。
**注意**,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。
你可以返回 **任意有效的结果** 。
【解答】
public TreeNode insertIntoBST(TreeNode root, int val) {
if(null==root){
return new TreeNode(val);
}
if(val>root.val){
if(root.right==null){
root.right = new TreeNode(val);
}else{
insertIntoBST(root.right,val);
}
}else{
if(root.left==null){
root.left = new TreeNode(val);
}else{
insertIntoBST(root.left,val);
}
}
return root;
}
二叉搜索树修改与构造题1:108.将有序数组转换为二叉搜索树
【题目】
给你一个整数数组 `nums` ,其中元素已经按 **升序** 排列,请你将其转换为一棵平衡二叉搜索树。
【解答】
public TreeNode sortedArrayToBST(int[] nums) {
return sortedArrayToBST(nums,0,nums.length-1);
}
public TreeNode sortedArrayToBST(int[] nums,int left,int right){
if(left>right){
return null;
}
int mid = (left+right)/2;
TreeNode root = new TreeNode(nums[mid]);
root.left = sortedArrayToBST(nums,left,mid-1);
root.right = sortedArrayToBST(nums,mid+1,right);
return root;
}