刷题04

左叶子之和

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root==null) return 0;
        int result=0;
        if(root.left!=null&&root.left.left==null&&root.left.right==null)
        {
            result=result+root.left.val;
        }
        return sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right)+result;
    }
}

树最左下角的值

class Solution {
    public int findBottomLeftValue(TreeNode root) {
       if(root==null) return 0;
       int result=0;
       Queue<TreeNode> queue=new LinkedList<>();
       queue.add(root);
       while(!queue.isEmpty())
       {
           int t=queue.size();
           TreeNode temp;
           for(int i=0;i<t;i++)
           {
               temp=queue.poll();
               if(i==0) result=temp.val;
               if(temp.left!=null) queue.add(temp.left);
               if(temp.right!=null) queue.add(temp.right);
           }
       }
       return result;
    }
}

最大二叉树

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return construct(nums,0,nums.length-1);
    }
    public TreeNode construct(int[] nums,int ns,int ne)
    {
        if(ns>ne) return null;
        int index=ns;
        int curroot=Integer.MIN_VALUE;
        for(int i=ns;i<=ne;i++)
        {
            if(nums[i]>curroot)
            {
                curroot=Math.max(curroot,nums[i]);
                index=i;
            }
            
        }
        TreeNode root=new TreeNode(curroot);
        root.left=construct(nums,ns,index-1);
        root.right=construct(nums,index+1,ne);
        return root;
    }
}

合并二叉树

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1==null) return root2;
        if(root2==null) return root1;
        TreeNode root=new TreeNode(root1.val+root2.val);
        root.left=mergeTrees(root1.left,root2.left);
        root.right=mergeTrees(root1.right,root2.right);
        return root;
    }
}

二叉树搜索树中的搜素

class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if(root==null) return null;
        if(root.val<val)
        {
            return searchBST(root.right,val);
        }
        else if(root.val>val)
        {
            return searchBST(root.left,val);
        }
        else{
            return root;
        }
    }
}

验证二叉搜索树

class Solution {
    public long pre=Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if(root==null) return true;
        if(!isValidBST(root.left))
        {
            return false;
        }
        if(root.val<=pre)
        {
            return false;
        }
        pre=root.val;
        return isValidBST(root.right);
    }
    
}

二叉搜索树中的最小绝对值差

class Solution {
    TreeNode temp=null;
    int sum=Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        if(root==null) return 0;
        getMin(root);
        return sum;
    }
    public void getMin(TreeNode root)
    {
        if(root==null) return;
        getMin(root.left);
        if(temp!=null)
        {
            sum=Math.min(sum,Math.abs(temp.val-root.val));
        }
        temp=root;
        getMin(root.right);
    }
}

二叉搜索树中的众数

class Solution {
    int cur,max;
    TreeNode pre;
    List<Integer> sum=new LinkedList<>();
    public int[] findMode(TreeNode root) {
        getMany(root);
        return sum.stream().mapToInt(Integer::valueOf).toArray();
    }
    public void getMany(TreeNode root)
    {
        if(root==null) return;
        getMany(root.left);
        if(pre!=null&&root.val==pre.val)
        {
            cur++;
        }
        else if(pre==null||root.val!=pre.val)
        {
            cur=1;
        }
        if(cur>max)
        {
            max=cur;
            sum.clear();
            sum.add(root.val);
        }else if(cur==max){
            sum.add(root.val);
        }
        pre=root;
        getMany(root.right);
    }
}

二叉树的最近公共祖先

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

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

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

二叉搜索树插入

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root==null) return new TreeNode(val);
        if(root.val>val)
        {
            root.left=insertIntoBST(root.left,val);
        }
        else if(root.val<val)
        {
            root.right=insertIntoBST(root.right,val);
        }
        return root;
    }
}

二叉搜索树转换为累加树

class Solution {
    int result=0;
    public TreeNode convertBST(TreeNode root) {
      getSum(root);
      return root;
    }
    public void getSum(TreeNode root)
    {
        if(root==null) return;
        getSum(root.right);
        result=result+root.val;
        root.val=result;
        getSum(root.left);
    }
}

有序数组构造二叉搜索树

class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
       return construct(nums,0,nums.length-1);
    }
    public TreeNode construct(int[] nums,int ns,int ne)
    {
        if(ns>ne) return null;
        int mid=ns+((ne-ns)>>1);
        TreeNode root=new TreeNode(nums[mid]);
        root.left=construct(nums,ns,mid-1);
        root.right=construct(nums,mid+1,ne);
        return root;
    }
}

修剪二叉树

class Solution {
    public TreeNode trimBST(TreeNode root, int low, int high) {
       if(root==null) return null;
       if(root.val>high)
       {
           return trimBST(root.left,low,high);
       }
       if(root.val<low)
       {
           return trimBST(root.right,low,high);
       }
       root.left=trimBST(root.left,low,high);
       root.right=trimBST(root.right,low,high);
       return root;
    }
}
posted @ 2022-02-04 09:00  一刹流云散  阅读(21)  评论(0编辑  收藏  举报