java数据结构之二叉树(3)

二叉搜索树的最小绝对差

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    int pre;
    int ans;
    public int getMinimumDifference(TreeNode root) {
        pre = -1;
        ans = Integer.MAX_VALUE;
        helper(root);
        return ans;
    }
    //二叉搜索树的特点就是 中序遍历 单调递增
    // 当有解决不了的地方 可以尝试设置一个标志位

    public void helper(TreeNode node){
        if(node==null) return ;
        helper(node.left);
        if(pre==-1){
            pre=node.val;
        }else{
            ans = Math.min(ans,node.val-pre);
            pre = node.val;
        } 
        helper(node.right);
    }

二叉搜素树中的众数

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    Deque<Integer> stack = new LinkedList<>();
    int base, count, maxCount;
    public int[] findMode(TreeNode root) {
        dfs(root);
        int[] ans=new int[stack.size()]; 
        int idx=0;
        for(int q:stack){
            ans[idx++]=q;
        }
        return ans;   
    }

    public void dfs(TreeNode root){
        if(root==null){
            return ;
        }
        dfs(root.left);
        // 逻辑语句
        upadte(root.val);


        dfs(root.right);
    }

    //更新众数
    public void upadte(int x){
        if(base==x){
            ++count;
        }else{
            count =1;
            base=x;
        }
        if(count==maxCount){
            stack.add(base);
        }
        if(count>maxCount){
            stack.clear();
            maxCount=count;
            stack.add(base);
        }
    }
}

二叉树的最近公共祖先

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        return helper(root,p,q);
        
    }
    // 做二叉树的问题 一般要遍历  那么首先就要确定是前中后哪种遍历


    public TreeNode helper(TreeNode root,TreeNode p ,TreeNode q){
         
        if(root==p || root ==q || root==null) return root;
        TreeNode left = helper(root.left,p,q);
        TreeNode right = helper(root.right,p,q);

        if(left!=null && right!=null) return root;
        if(left!=null && right==null) return left;
        else if(left==null && right!=null) return right;
        else{
            return null;
        }

    }
}

二叉搜索树的最近公共祖先

在这里插入图片描述

递归
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        return dfs(root,p,q);
        
    }
    public TreeNode dfs(TreeNode node , TreeNode p,TreeNode q){
        if(node == null ) return null;
        if(node.val>q.val && node.val>p.val){
            TreeNode left = dfs(node.left,p,q);
            if(left!=null){
                return left;
            }
        }
        if(node.val<q.val && node.val<p.val){
            TreeNode right = dfs(node.right,p,q);
            if(right!=null){
                return right;
            }
        }
        return node; 
        
    }
}
迭代

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        while(root!=null){
            if(root.val>q.val&&root.val>p.val){
                root=root.left;
            }else if(root.val<q.val &&root.val<p.val){
                root=root.right;
            }else{
                return root;
            }
        }
        return null;
    }
}

二叉搜索树中的插入操作

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        return helper(root,val);
    }
    public TreeNode helper(TreeNode root,int val){
        if(root == null){
            TreeNode node= new TreeNode(val);
            return node;
        }
        if(root.val>val) root.left=helper(root.left,val);
        if(root.val<val) root.right=helper(root.right,val);
        return root;
    }
}
迭代
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root==null){
            TreeNode node = new TreeNode(val);
            return node;
        }
        TreeNode parent = root;
        TreeNode son = root;
        while(son!=null){
            parent=son;
            if(son.val>val) son=son.left;
            else son=son.right;
        }
        TreeNode node = new TreeNode(val);
        if(val<parent.val) parent.left=node;
        else parent.right=node;
        return root;
    }
}

删除搜素二叉树中的节点

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {

        return helper(root,key);
        
    }
    // 如果这个结点的左右孩子都为空 则直接删除
    //如果这个结点左孩子不为空  右孩子为空 则将左孩子置为当前根结点
    //如果这个节点的右孩子不为空 左孩子为空  则将右孩子置为当前结点
    //如果左右孩子都不为空 则将当前结点的左子树 放在右子树最左边的点作为左子树

    public TreeNode helper(TreeNode node , int key){
        if(node==null) return null;
        if(node.val==key){
            if(node.left==null&&node.right==null) return null;
            else if(node.left!=null&&node.right==null) return node.left;
            else if(node.right!=null&&node.left==null) return node.right;
            else{
                TreeNode temp = node.right;
                while(temp.left!=null){
                    temp=temp.left;
                }
                temp.left=node.left;
                node=node.right;
                return node;
            }
        }
        if(node.val>key){
            node.left=helper(node.left,key);
        }
        if(node.val<key){
            node.right=helper(node.right,key);
        }
        return node;
    }
}

修剪二叉树

在这里插入图片描述

迭代
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode trimBST(TreeNode root, int low, int high) {
        if(root==null) return null;

        //处理头节点 让头节点走到范围里
        while(root.val<low || root.val>high){
            if(root.val<low) root=root.right;
            else root=root.left;
            if(root==null) return null;
        }

        TreeNode root2 = root;
        while(root2!=null){
            while(root2.left!=null&&root2.left.val<low){
                root2.left=root2.left.right;
            }
            root2=root2.left;
        }
        root2=root;

        while(root2!=null){
            while(root2.right!=null&&root2.right.val>high){
                root2.right=root2.right.left;
            }
            root2=root2.right;
        }
        return root;
    }
}
递归 (一入递归深似海啊)
class Solution {
    public TreeNode trimBST(TreeNode root, int low, int high) {
        return helper(root,low,high);
    }
    public TreeNode helper(TreeNode node,int low,int high){
        if(node==null) return null;
        if(node.val<low) return helper(node.right,low,high);
        if(node.val>high) return helper(node.left,low,high);
        node.left=helper(node.left,low,high);
        node.right=helper(node.right,low,high);
        return node;
    } 
}

将有序数组转换为二叉搜索树

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return dfs(nums,0,nums.length-1);
    }
    //递归三部曲:返回值是什么   输入的参数是什么
    // 终止语句是什么
    //语言逻辑是什么
    public TreeNode dfs(int[] nums,int left , int right){
        if(left>right) return null;
        int mid = left+(right-left)/2;
        TreeNode node = new TreeNode(nums[mid]);
        node.left=dfs(nums,left,mid-1);
        node.right=dfs(nums,mid+1,right);
        return node;  
    }
}
迭代

这个迭代不太懂 — 噢噢噢噢
这个如果用五个队列就好懂了
一个放结点
两个放下表的值
两个放上表的值

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        if(nums.length==0) return null;
        TreeNode root = new TreeNode(0);
        Deque<TreeNode> nodeQue = new LinkedList<>();
        Deque<Integer> leftQue = new LinkedList<>();
        Deque<Integer> rightQue = new LinkedList<>();
        nodeQue.push(root);
        leftQue.push(0);
        rightQue.push(nums.length-1);

        while(!nodeQue.isEmpty()){
            TreeNode curnode = nodeQue.pop();
            int left = leftQue.pop();
            int right = rightQue.pop();
            int mid = left+(right-left)/2;

            curnode.val=nums[mid];

            if(left<=mid-1){
                curnode.left=new TreeNode(0);
                nodeQue.push(curnode.left);
                leftQue.push(left);
                rightQue.push(mid-1);
            }
            if(right>=mid+1){
                curnode.right=new TreeNode(0);
                nodeQue.push(curnode.right);
                leftQue.push(mid+1);
                rightQue.push(right);
            }
        }
        return root;
    }
}

把二叉搜索树转换为累加树

在这里插入图片描述
在这里插入图片描述

递归
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int pre;
    public TreeNode convertBST(TreeNode root) {
        pre = 0;
        dfs(root);
        return root;

    }
    // 反中序遍历
    public void dfs(TreeNode root){
        if(root==null) return;
        dfs(root.right);
        root.val+=pre;
        pre=root.val;
        dfs(root.left);
    }
}
迭代
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    public TreeNode convertBST(TreeNode root) {
        if(root==null) return null;
        int pre=0;;
        Deque<TreeNode> stack = new LinkedList<>();
        TreeNode node = root;
        while(!stack.isEmpty() || node!=null){
            while(node!=null){
                stack.push(node);
                node=node.right;
            }
            
            node = stack.pop();
            node.val+=pre;
            pre=node.val;
            node=node.left;
            
        }
        return root;
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    public TreeNode convertBST(TreeNode root) {
        if(root==null) return null;
        int pre=0;;
        Deque<TreeNode> stack = new LinkedList<>();
        TreeNode node = root;
        while(!stack.isEmpty() || node!=null){
            if(node!=null){
                stack.push(node);
                node=node.right;
            }else{
                node = stack.pop();
                node.val+=pre;
                pre=node.val;
                node=node.left;
            }
        }
        return root;
    }
}

二叉树总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值