力扣labuladong——一刷day63

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣1080. 根到叶路径上的不足节点
  • 二、力扣117. 填充每个节点的下一个右侧节点指针 II
  • 三、力扣662. 二叉树最大宽度


前言


二叉树大部分题目都可以用递归的算法解决,但少部分题目用递归比较麻烦的话,我们可以考虑使用层序遍历的方式解决。

一、力扣1080. 根到叶路径上的不足节点

/**
 * 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 sufficientSubset(TreeNode root, int limit) {
        if(root == null){
            return null;
        }
        if(root.left == null && root.right == null){
            if(root.val < limit){
                return null;
            }
            return root;
        }
        TreeNode left = sufficientSubset(root.left,limit - root.val);
        TreeNode right = sufficientSubset(root.right, limit - root.val);
        if(left == null && right == null){
            return null;
        }
        root.left = left;
        root.right = right;
        return root;
    }
}

二、力扣117. 填充每个节点的下一个右侧节点指针 II

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}
    
    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
*/

class Solution {
    public Node connect(Node root) {
        if(root == null){
            return null;
        }
        Deque<Node> deq = new ArrayDeque<>();
        deq.offerLast(root);
        while(!deq.isEmpty()){
            int size = deq.size();
            Node pre = deq.peekFirst();
            for(int i = 0; i < size; i ++){
                Node cur = deq.pollFirst();
                if(cur.left != null){
                    deq.offerLast(cur.left);
                }
                if(cur.right != null){
                    deq.offerLast(cur.right);
                }
                if(i >0){
                    pre.next = cur;
                    pre = cur;
                }
            }
        }
        return root;
    }
}

三、力扣662. 二叉树最大宽度

/**
 * 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 {
    class Pair{
        TreeNode node;
        int id;
        public Pair(TreeNode node, int id){
            this.node = node;
            this.id = id;
        }
    }
    public int widthOfBinaryTree(TreeNode root) {
        Deque<Pair> deq = new ArrayDeque<>();
        deq.offerLast(new Pair(root,1));
        int res = 1;
        while(!deq.isEmpty()){
            int size = deq.size();
            int low = 0, high = 0;
            for(int i = 0; i < size; i ++){
                Pair cur = deq.pollFirst();
                TreeNode curNode = cur.node;
                int curId = cur.id;
                if(curNode.left != null){
                    deq.offerLast(new Pair(curNode.left,curId*2));
                }
                if(curNode.right != null){
                    deq.offerLast(new Pair(curNode.right,curId*2+1));
                }
                if(i == 0){
                    low = curId;
                }
                if(i == size-1){
                    high = curId;
                }
            }
            res = Math.max(res, high-low+1);
        }
        return res;
    }
}

你可能感兴趣的:(力扣题解,leetcode,算法,数据结构,java)