00第七节课笔记

本文介绍了四个关于二叉树的LeetCode题目,包括按层遍历收集节点、判断是否为平衡搜索二叉树、验证搜索二叉树以及检查二叉树路径和。每个问题都提供了Java实现的解决方案,涉及队列、递归等算法技巧。
摘要由CSDN通过智能技术生成

二叉树练习

1、二叉树按层遍历并收集节点

Leetcode原题,https://leetcode.com/problems/binary-tree-level-order-traversal-ii

public static List fangfa(Node root){
        LinkedList<LinkedList<Integer>> linkedLists = new LinkedList<>();
        if (root == null) return linkedLists;

        Queue queue = new LinkedList();
        queue.add(root);

        while (!queue.isEmpty()){
            LinkedList list = new LinkedList();
            int size = queue.size();
            for (int i = 0; i < size; i++){
                Node p = (Node) queue.poll();
                list.add(p.value);
                if (p.left!=null) queue.add(p.left);
                if (p.right!=null) queue.add(p.right);
            }
            linkedLists.add(0, list);
        }
        return linkedLists;

    }

2、判断是否是平衡搜索二叉树

Leetcode原题,https://leetcode.com/problems/balanced-binary-tree

用第二个类来记录

public static class Info{
        boolean balance;
        int height;
        public Info(){

        }
        public Info(boolean balance, int height){
            this.balance = balance;
            this.height = height;
        }
    }

    public static Info isBalance(Node root){
        if (root == null){
            return new Info(true, 0);
        }
        Info ifo = new Info();
        ifo.height = Math.max(isBalance(root.left).height, isBalance(root.right).height)+1;
        ifo.balance = Math.abs(isBalance(root.left).height - isBalance(root.right).height)<2;
        return ifo;
    }

3、判断二叉树是否是搜索二叉树

    public static Info sousuoerchashu(Node root){
        if (root == null) return null;
        Info ssh =  new Info();
        Info ss1 = sousuoerchashu(root.left);
        Info ss2 = sousuoerchashu(root.right);
        if (ss1 != null && ss2 != null){
            ssh.min = ss1.max;
            ssh.max = ss2.min;
            if (ssh.min < root.value && ssh.max > root.value){
                ssh.sousuo = true;
            }else ssh.sousuo = false;
        }
        else if (ss1 == null && ss2 != null){
            ssh.max = ss2.min;
            ssh.min = root.value;
            if (root.value < ss2.min){
                ssh.sousuo = true;
            }else ssh.sousuo = false;

        }
        else if (ss1 != null && ss2 == null){
            ssh.min = ss1.max;
            ssh.max = root.value;
            if (root.value > ss1.max){
                ssh.sousuo = true;
            }else ssh.sousuo = false;
        }else {
            ssh.min = root.value;
            ssh.max = root.value;
            ssh.sousuo = true;
        }
        return ssh;
    }

    public static class Info{
        boolean sousuo;
        int min;
        int max;
        public Info(){

        }
        public Info(boolean sousuo){
            this.sousuo = sousuo;
        }
        public Info(int min, int max){
            this.min = min;
            this.max = max;
        }
    }

4、在二叉树上能否组成路径和

Leetcode原题,https://leetcode.com/problems/path-sum

public static boolean f(Node head, int preSum, int sum){
        if (head == null && preSum == sum) return true;
        else if (head == null && preSum != sum) return false;
        else if (head != null && preSum == sum) return false;
        else {
            if (f(head.left, preSum+head.value, sum)) return true;
            if (f(head.right, preSum+head.value, sum)) return true;
            else return false;
        }
    }

5、在二叉树上收集所有达标的路径和

注意集合的复制!!!!

public static void f2(Node head, int preSum, int sum, List list, List<List> lists){
    if (head.left == null && head.right == null){
        if (preSum+head.value == sum){
            list.add(head.value);
            lists.add(copy1(list));
            list.remove(list.size()-1);
        }
        return;
    }
    preSum+=head.value;
    list.add(head.value);

    if (head.left!=null){
        f2(head.left, preSum,  sum, list, lists);
    }
    if (head.right!=null){
        f2(head.right, preSum, sum, list, lists);
    }

    list.remove(list.size()-1);
    return;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值