leetcode二叉树专题总结(二)

1.给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) return res;
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            List<Integer> list = new ArrayList();
            int size = queue.size();
            while(size-- > 0){
                TreeNode node = queue.poll();
                if(node.left != null){
                    queue.offer(node.left);
                }
                if(node.right != null){
                    queue.offer(node.right);
                }
                list.add(node.val);
            }
            res.add(0,list);
            //res.addFirst(list);往队头添加
        }
        return res;
    }
}

2.给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。

class Solution {
    //第一种方法——递归
    public int maxDepth(TreeNode root){
        if(root == null) return 0;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left, right)+1;
    }
    //第二种方法——BFS层序遍历
    // public int maxDepth(TreeNode root) {
    //     int i = 0;
    //     if(root == null) return i;
    //     Queue<TreeNode> queue = new LinkedList();
    //     queue.offer(root);
    //     while(!queue.isEmpty()){
    //         int size = queue.size();
    //         while(size-- > 0){
    //             TreeNode node = queue.poll();
    //             if(node.left != null){
    //                 queue.offer(node.left);
    //             }
    //             if(node.right != null){
    //                 queue.offer(node.right);
    //             }
    //         }
    //         i++;
    //     }
    //     return i;
    // }
}

3.根据一棵树的前序遍历与中序遍历构造二叉树。

class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(preorder.length == 0 || inorder.length == 0) return null;
        return creat(0, preorder.length-1, 0, inorder.length-1, preorder, inorder);
    }
    private TreeNode creat(int preL, int preR, int inL, int inR, int[] preorder, int[] inorder){
        if(preL > preR){
            return null;//先序序列长度小于等于0时,直接返回
        }
        TreeNode root = new TreeNode(preorder[preL]);//新建一个新的结点,用来存放当前二叉树的根节点,新节点的数据域为根节点的值
        int k;
        for(k = inL; k <= inR; k++){
            if(inorder[k] == preorder[preL]){//在中序序列中找到inorder[k] == preorder[preL]
                break;
            }
        }
        int numLeft = k - inL;//左子树的节点个数

        //左子树的先序区间为[preL+1, preL+numLeft],中序区间为[inL, k-1]
        //返回左子树的根节点地址,赋值给root的左指针
        root.left = creat(preL + 1, preL + numLeft, inL, k - 1, preorder, inorder);

        //右子树的先序区间为[preL+numLeft+1,preR],中序区间为[k+1, inR]
        //返回右子树的根节点地址,赋值给root的右指针
        root.right = creat(preL + numLeft + 1, preR, k+1, inR, preorder, inorder);

        return root;//返回根节点
    }
}

4.根据一棵树的中序遍历与后序遍历构造二叉树。

class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        return creat(0, postorder.length-1, 0, inorder.length-1,inorder, postorder);

    }
    private TreeNode creat(int postL, int postR, int inL, int inR, int []inorder, int[] postorder){
        if(postL > postR){
            return null;//后序序列长度小于0,直接返回
        }
        TreeNode root = new TreeNode(postorder[postR]);//新建一个新的结点,用来存放当前二叉树的根节点,新节点的数据域为根节点的值

        int k = 0;
        for(k = inL; k < inR; k++){
            if(inorder[k] == postorder[postR]){
                break;
            }
        }

        int numleft = k - inL;//左子树节点的个数

        root.left = creat(postL, postL+numleft-1, inL, k-1, inorder, postorder);

        root.right = creat(postL+numleft, postR-1, k+1, inR, inorder, postorder);

        return root;
    }
}

4.将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。
本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。

class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return nums == null ? null :buildTree(nums, 0, nums.length-1);
    }
    private TreeNode buildTree(int[] nums, int l, int r){
        if(l > r){
            return null;
        }
        int m = l + (r - l) / 2;
        TreeNode root = new TreeNode(nums[m]);
        root.left = buildTree(nums, l, m - 1);
        root.right = buildTree(nums, m + 1, r);
        return root; 
    }
}

5.给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。
本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。

class Solution {
    public TreeNode sortedListToBST(ListNode head) {
		if(head == null) return null;
		else if(head.next == null) return new TreeNode(head.val);
        ListNode pre = head;
        ListNode p = pre.next;
        ListNode q = p.next;
        //找到链表的中点
        while(q != null && q.next != null){
            pre =pre.next;
            p = p.next;
            q = q.next.next;
        }
        //将链表分成俩个部分
        pre.next = null;
        TreeNode root = new TreeNode(p.val);
        root.left = sortedListToBST(head);
        root.right = sortedListToBST(p.next);
        return root;

    }
}

6.给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。

class Solution {
    public boolean isBalanced(TreeNode root) {
        return height(root) >= 0;
    }
    private int height(TreeNode root){
        if(root == null) return 0;
        int lh = height(root.left),rh = height(root.right);
        if(lh >= 0 && rh >= 0 && Math.abs(lh - rh) <= 1){
            return Math.max(lh, rh) + 1;
        }else{
            return -1;
        }
    }
}

7.给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。

class Solution {
    public int minDepth(TreeNode root) {
        //求最大深度
        // if(root == null){
        //     return 0;
        // }
        // return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
        if(root == null){
            return 0;
        }
        //null节点不参与比较
        if(root.left == null && root.right != null){
            return 1+minDepth(root.right);
        }
        if(root.right == null && root.left != null){
            return 1+minDepth(root.left);
        }
        return 1+Math.min(minDepth(root.left),minDepth(root.right));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值