2021-09-05

求多叉树最大深度
class Solution {
    public int maxDepth(Node root) {
        if(root==null) return 0;
        int y=0;
        for(Node x : root.children)
        {
            y=Math.max(maxDepth(x),y);
        }
        return y+1;
    }
}
二叉树的坡度
class Solution {
    int sum=0;
    public int findTilt(TreeNode root) {
        if(root ==null) return 0;
        sum=sum+cal2(root);
        findTilt(root.left);
        findTilt(root.right);
        return sum;
    }
    public int cal2(TreeNode root)
    {
        int sum=0;
        if(root ==null) return 0;
        sum=Math.abs(cal(root.left)-cal(root.right));
        return sum;
    }
    public int cal(TreeNode root)
    {
        int sum=0;
        if(root ==null) return 0;
        sum=sum+cal(root.left)+cal(root.right);
        return sum+root.val;
    }
}

多叉树的前序遍历
class Solution {
    List<Integer> sum=new LinkedList<>();
    public List<Integer> preorder(Node root) {
        if(root==null) return sum;
        sum.add(root.val);
        cal(root);
        return sum;
    }
    public List<Integer> cal(Node root) {
        if(root==null) return sum;
        for(Node x : root.children)
        {
            sum.add(x.val);
            cal(x);
        }
        return sum;
    }
}

多叉树的后序遍历
class Solution {
   List<Integer> sum=new LinkedList<>();
    public List<Integer> postorder(Node root) {
        if(root==null) return sum;
        cal(root);
        sum.add(root.val);
        return sum;
    }
    public List<Integer> cal(Node root) {
        if(root==null) return sum;
        for(Node x : root.children)
        {
            cal(x);
            sum.add(x.val);
        }
        return sum;
    }
}

二叉树的层平均值
class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> cal=new LinkedList<>();
        List<List<Double>> sum=new LinkedList<>();
        Double temp;
        if(root==null) return cal;
        sum=level(root);
        for(List<Double> x : sum)
        {
            temp=0.0;
            for(Double y : x)
            {
                temp=temp+y;
            }
            temp=temp/x.size();
            cal.add(temp);
        }
        return cal;
    }
    public List<List<Double>> level(TreeNode root)
    {
        List<List<Double>> sum=new LinkedList<>();
        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null) return sum;
        queue.add(root);
        while(!queue.isEmpty())
        {
            List<Double> x=new ArrayList<>();
            TreeNode temp;
            int t=queue.size();
            for(int i=0;i<t;i++)
            {
                temp=queue.poll();
                x.add(Double.valueOf(temp.val));
                if(temp.left!=null) queue.add(temp.left);
                if(temp.right!=null) queue.add(temp.right);
            }
            sum.add(x);
        }
        return sum;

    }
}

二叉搜索树节点的最小距离
class Solution {
    TreeNode prenode=null;
    int min=1000;
    public int minDiffInBST(TreeNode root) {
        if(root==null) return 0;
        cal(root);
        return min;
    }
    public void cal(TreeNode root)
    {
        if(root==null) return;
        cal(root.left);
        if(prenode!=null)
        {
            min=Math.min(root.val-prenode.val,min);
        }
        prenode=root;
        cal(root.right);
    }
}

递增顺序二叉树
class Solution {
    TreeNode prenode=null;
    int k=0;
    TreeNode t;
    public TreeNode increasingBST(TreeNode root) {
        if(root==null)  return null;
        if(root.left==null&&root.right==null)
        {
            return root;
        }
        cal(root);
        return t;
    }
    public void cal(TreeNode root)
    {
        if(root==null) return;
        cal(root.left);
        TreeNode x= new TreeNode(root.val);
        if(prenode!=null)
        {
            prenode.right=x;
            k++;
            if(k==1)
            {
                t=prenode;
            }
        }
        prenode=x;
        cal(root.right);
    }
}

二叉搜索树的范围和
class Solution {
    int sum=0;
    public int rangeSumBST(TreeNode root, int low, int high) {
        if(root==null) return 0;
        rangeSumBST(root.left,low,high);
        if(root.val>=low&&root.val<=high)
        {
            sum=sum+root.val;
        }
        rangeSumBST(root.right,low,high);
        return sum;
    }
}
单值二叉树
class Solution {
    int x;
    int k=0;
    public boolean isUnivalTree(TreeNode root) {
        if(root==null) return true;
        k++;
        if(k==1)   x=root.val;
        if(root.val!=x)
        {
            return false;
        }
        return isUnivalTree(root.right)&&isUnivalTree(root.left);
    }
}

前序遍历构造二叉搜索树
class Solution {
    public TreeNode bstFromPreorder(int[] preorder) {
        if(preorder==null||preorder.length<1)   return null;	
        int[] inorder=new int[preorder.length];
        for(int i=0;i<preorder.length;i++){
            inorder[i]=preorder[i];
        }
        Arrays.sort(inorder);
        return cal(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
    }
    public TreeNode cal(int[] preorder,int ps,int pe,int[] inorder,int is,int ie)
    {
        if(ps>pe||is>ie) return null;
        int curroot=preorder[ps];
        int index=is;
        while(index<=ie&&curroot!=inorder[index])
        {
            index++;
        }
        if(index>ie)
        {
            return null;
        }
        TreeNode node=new TreeNode(curroot);
        node.left=cal(preorder,ps+1,pe-is+index,inorder,is,index-1);
        node.right=cal(preorder,ps+1+index-is,pe,inorder,index+1,ie);
        return node;
    }
}

class Solution {
    public TreeNode bstFromPreorder(int[] preorder) {
        if(preorder==null||preorder.length<1)   return null;	
        TreeNode root=null;
        for(int i=0;i<preorder.length;i++)
        {
            root=cal(root,preorder[i]);
        }
        return root;
    }
    public TreeNode cal(TreeNode root,int curroot)
    {
        if(root==null) return new TreeNode(curroot);
        if(curroot>root.val)
        {
            root.right=cal(root.right,curroot);
        }
        else
        {
            root.left=cal(root.left,curroot);
        }
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用python中的pymsql完成如下:表结构与数据创建 1. 建立 `users` 表和 `orders` 表。 `users` 表有用户ID、用户名、年龄字段,(id,name,age) `orders` 表有订单ID、订单日期、订单金额,用户id字段。(id,order_date,amount,user_id) 2 两表的id作为主键,`orders` 表用户id为users的外键 3 插入数据 `users` (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28); `orders` (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4); 查询语句 1. 查询订单总金额 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 3. 查询订单总数最多的用户的姓名和订单总数。 4. 查询所有不重复的年龄。 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 8. 查询订单总金额最大的用户的姓名和订单总金额。 9. 查询订单总金额最小的用户的姓名和订单总金额。 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。
06-03

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值