二叉树类型
关于二叉树类型中的题,那些递归的内容大家千万别陷进去,因为你用你的小脑袋瓜去递归这些东西只会让你越来越迷糊,我们只要考虑某一个节点 的情况就可以,剩下的递归交给计算机就可以了。
class Solution {
public TreeNode invertTree(TreeNode root) {
//base case
if(root==null)return null;
//交换左右子节点
TreeNode tmp=root.left;
root.left=root.right;
root.right=tmp;
//让子节点的子节点进行交换
invertTree(root.left);
invertTree(root.right);
return root;
}
}
class Solution {
//主函数
public Node connect(Node root) {
if(root==null) return null;
connectTwoNode(root.left,root.right);
return root;
}
//辅助方法
void connectTwoNode(Node root1,Node root2){
if(root1==null||root2==null){
return;
}
//先连接传进来的两个子节点
root1.next=root2;
//分别连接两个子节点下面的子节点
connectTwoNode(root1.left,root1.right);
connectTwoNode(root2.left,root2.right);
//连接左右子节点交汇处的节点
connectTwoNode(root1.right,root2.left);
}
}
class Solution {
public void flatten(TreeNode root) {
//base case
if(root==null)return;
flatten(root.left);
flatten(root.right);
//1.左右子树被拉成一个链条
TreeNode nodeLeft=root.left;
TreeNode nodeRight=root.right;
//将左子树赋到右子树
root.left=null;
root.right=nodeLeft;
//将原先的左子树接到现在的右子树后面
TreeNode p=root;
while(p.right!=null){
p=p.right;
}
p.right=nodeRight;
}
}
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return build(nums,0,nums.length-1);
}
TreeNode build(int[] nums,int lo,int hi){
//如果传进来的最低的下标大于最大的说明数组为空
if(lo>hi)return null;
//假设最大值的下标为index
int index=-1;
int max=Integer.MIN_VALUE;
for(int i=lo;i<=hi;i++){
if(nums[i]>max){
max=nums[i];
index=i;
}
}
TreeNode root= new TreeNode(max);
//递归子树
root.left=build(nums,lo,index-1);
root.right=build(nums,index+1,hi);
return root;
}
}
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return build(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
}
TreeNode build(int[] preorder,int preStart,int preEnd,int[] inorder,int inStart,int inEnd){
//base case
if(preStart>preEnd)return null;
//root 节点的值为前序遍历的第一个元素
int rootVal=preorder[preStart];
int index=0;
for(int i=0;i<preorder.length;i++){
if(rootVal==inorder[i]){
index=i;
break;
}
}
int leftLength=index-inStart;
TreeNode root=new TreeNode(rootVal);
root.left=build(preorder,preStart+1,preStart+leftLength,inorder,inStart,index-1);
root.right=build(preorder,preStart+leftLength+1,preEnd,inorder,index+1,inEnd);
return root;
}
}
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return build(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
}
TreeNode build(int[] inorder,int inStart,int inEnd,int[] postorder,int postStart,int postEnd){
//base case
if(inStart>inEnd)return null;
//根节点就是后序遍历的最后一位
int rootVal=postorder[postEnd];
int index=0;
for(int i=inStart;i<=inEnd;i++){
if(rootVal==inorder[i]){
index=i;
break;
}
}
int leftSize=index-inStart;
TreeNode root =new TreeNode(rootVal);
root.left=build(inorder,inStart,index-1,postorder,postStart,postStart+leftSize-1);
root.right=build(inorder,index+1,inEnd,postorder,postStart+leftSize,postEnd-1);
return root;
}
}
class Solution {
//记录出现重复的子树的集合
List<TreeNode> res=new ArrayList<>();
//记录所有子树和出现的次数
HashMap<String,Integer> memo=new HashMap<>();
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
traverse(root);
return res;
}
/*辅助函数*/
String traverse(TreeNode root){
if(root==null){
return "#";
}
String left=traverse(root.left);
String right=traverse(root.right);
//后序遍历
String subTree=left+","+right+","+root.val;
//出现的频率,如果不存在默认为0
int freq=memo.getOrDefault(subTree,0);
//如果存在1个就添加进集合中
if(freq==1){
res.add(root);
}
memo.put(subTree,freq+1);
return subTree;
}
}
class Solution {
//结果
int res=0;
//元素排名
int rank=0;
public int kthSmallest(TreeNode root, int k) {
traverse(root,k);
return res;
}
/*中序遍历,中序遍历的结果是按照从小到大排的*/
void traverse(TreeNode root,int k){
if(root==null){
return;
}
traverse(root.left,k);
rank++;
if(k==rank){
res=root.val;
return;
}
traverse(root.right,k);
}
}
class Solution {
//记录累加和
int sum=0;
public TreeNode convertBST(TreeNode root) {
traverse(root);
return root;
}
/*中序遍历,但是与之前的不同的是这次的是降序排列,那么这道题就变成了累加和,也就是说遍历顺序为:右子树——节点——左子树*/
void traverse(TreeNode root){
if(root==null){
return;
}
traverse(root.right);
sum+=root.val;
root.val=sum;
traverse(root.left);
}
}