代码随想录二刷day15

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

文章目录

  • 前言
  • 一、力扣102. 二叉树的层序遍历
  • 二、力扣107. 二叉树的层序遍历 II
  • 三、力扣199. 二叉树的右视图
  • 四、力扣637. 二叉树的层平均值
  • 五、力扣429. N 叉树的层序遍历
  • 六、力扣515. 在每个树行中找最大值
  • 七、力扣116. 填充每个节点的下一个右侧节点指针
  • 八、力扣117. 填充每个节点的下一个右侧节点指针 II
  • 九、力扣104. 二叉树的最大深度
  • 十、力扣111. 二叉树的最小深度
  • 十一、力扣226. 翻转二叉树
  • 十二、力扣101. 对称二叉树


前言


一、力扣102. 二叉树的层序遍历

/**
 * 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 List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Deque<TreeNode> deq = new ArrayDeque<>();
        List<Integer> li = new ArrayList<>();
        if(root == null)return res;
        deq.offerLast(root);
        TreeNode pre = root;
        while(!deq.isEmpty()){
            TreeNode p = deq.pollFirst();
            li.add(p.val);
            if(p.left != null)deq.offerLast(p.left);
            if(p.right != null)deq.offerLast(p.right);
            if(pre == p){
                res.add(li);
                li = new ArrayList();
                pre = deq.peekLast();
            }
        }
        return res;
    }
}

二、力扣107. 二叉树的层序遍历 II

/**
 * 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 List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Deque<TreeNode> deq = new ArrayDeque<>();
        if(root == null)return res;
        deq.offerLast(root);
        while(!deq.isEmpty()){
            int len = deq.size();
            List<Integer> li = new ArrayList<>();
            for(int i = 0; i < len; i ++){
                TreeNode p = deq.pollFirst();
                li.add(p.val);
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
            res.add(li);
        }   
        List<List<Integer>> r = new ArrayList<>(); 
        for(int i = res.size()-1; i >= 0; i--){
            r.add(res.get(i));
        }
        return r;
    }
}

三、力扣199. 二叉树的右视图

/**
 * 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 List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> deq = new ArrayDeque<>();
        if(root == null)return res;
        deq.offerLast(root);
        while(!deq.isEmpty()){
            int len = deq.size();
            for(int i = 0; i < len; i ++){
                TreeNode p = deq.pollFirst();
                if(i == len - 1){
                    res.add(p.val);
                }
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
        }
        return res;
    }
}

四、力扣637. 二叉树的层平均值

/**
 * 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 List<Double> averageOfLevels(TreeNode root) {
        List<Double> res = new ArrayList<>();
        Deque<TreeNode> deq = new ArrayDeque<>();
        deq.offerLast(root);
        while(!deq.isEmpty()){
            int len = deq.size();
            double count = 0;
            for(int i = 0; i < len; i ++){
                TreeNode p = deq.pollFirst();
                count += p.val;
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
            res.add(count / len);
        }
        return res;
    }
}

五、力扣429. N 叉树的层序遍历

/*
// Definition for a Node.
class Node {
    public int val;
    public List children;

    public Node() {}

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

    public Node(int _val, List _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList<>();
        Deque<Node> deq = new ArrayDeque<>();
        if(root == null)return res;
        deq.offerLast(root);
        while(!deq.isEmpty()){
            int len = deq.size();
            List<Integer> l = new ArrayList<>();
            for(int i = 0; i < len; i ++){
                Node p = deq.pollFirst();
                l.add(p.val);
                List<Node> li = p.children;
                for(Node n : li){
                    if(n != null)deq.offerLast(n);
                }
            }
            res.add(l);
        }
        return res;
    }
}

六、力扣515. 在每个树行中找最大值

/**
 * 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 List<Integer> largestValues(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Deque<TreeNode> deq = new ArrayDeque<>();
        if(root == null)return res;
        deq.offerLast(root);
        while(!deq.isEmpty()){
            int len = deq.size();
            int max = Integer.MIN_VALUE;
            for(int i = 0; i < len; i ++){
                TreeNode p = deq.pollFirst();
                max = max > p.val ? max:p.val;
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
            res.add(max);
        }
        return res;
    }
}

七、力扣116. 填充每个节点的下一个右侧节点指针

/*
// 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) {
        Deque<Node> deq = new ArrayDeque<>();
        if(root == null)return root;
        deq.offerLast(root);
        while(!deq.isEmpty()){
            int len = deq.size();
            for(int i = 0; i < len; i ++){
                Node p = deq.pollFirst();
                if(i != len-1){
                    Node ne = deq.peekFirst();
                    p.next = ne;
                }
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.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) {
        Deque<Node> deq = new ArrayDeque<>();
        if(root == null)return root;
        deq.offerLast(root);
        while(!deq.isEmpty()){
            int len = deq.size();
            for(int i = 0; i < len; i ++){
                Node p = deq.pollFirst();
                if(i != len-1){
                    Node ne = deq.peekFirst();
                    p.next = ne;
                }
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
        }
        return root;
    }
}

九、力扣104. 二叉树的最大深度

递归

/**
 * 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 int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int l = maxDepth(root.left);
        int r = maxDepth(root.right);
        return l > r ? l + 1: r + 1;
    }
}

迭代

/**
 * 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 int maxDepth(TreeNode root) {
        Deque<TreeNode> deq = new ArrayDeque<>();
        if(root == null)return 0;
        deq.offerLast(root);
        int high = 0;
        while(!deq.isEmpty()){
            int len = deq.size();
            for(int i = 0; i < len; i ++){
                TreeNode p = deq.pollFirst();
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
            high ++;
        }
        return high;
    }
}

十、力扣111. 二叉树的最小深度

/**
 * 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 int minDepth(TreeNode root) {
        Deque<TreeNode> deq = new ArrayDeque<>();
        if(root == null)return 0;
        deq.offerLast(root);
        int high = 0;
        while(!deq.isEmpty()){
            int len = deq.size();
            for(int i = 0; i < len; i ++){
                TreeNode p = deq.pollFirst();
                if(p.left == null && p.right == null){
                    return high + 1;
                }
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
            high ++;
        }
        return high;
    }
}

十一、力扣226. 翻转二叉树

迭代

/**
 * 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 invertTree(TreeNode root) {
        Deque<TreeNode> deq = new ArrayDeque<>();
        if(root == null)return root;
        deq.offerLast(root);
        while(!deq.isEmpty()){
            int len = deq.size();
            for(int i = 0; i < len; i ++){
                TreeNode p = deq.pollFirst();
                TreeNode temp = p.left;
                p.left = p.right;
                p.right = temp;
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
        }
        return root;
    }
}

递归

/**
 * 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 invertTree(TreeNode root) {
        if(root == null)return null;
        TreeNode chirlLeft = invertTree(root.left);
        TreeNode chirlRight = invertTree(root.right);
        TreeNode temp = chirlLeft;
        root.left = chirlRight;
        root.right = temp;
        return root;
    }
}

十二、力扣101. 对称二叉树

递归

/**
 * 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 boolean isSymmetric(TreeNode root) {
        Deque<TreeNode> deq = new ArrayDeque<>();
        if(root == null)return true;
        if(root.left == null && root.right == null)return true;
        if(root.left == null)return false;
        if(root.right == null)return false;
        if(root.left.val != root.right.val)return false;
        deq.offerLast(root.left);
        deq.offerLast(root.right);
        while(!deq.isEmpty()){
            int len = deq.size();
            if(len % 2 == 1)return false;
            TreeNode[] arr = new TreeNode[len];
            for(int i = 0; i < len; i ++){
                TreeNode p = deq.pollFirst();
                arr[i] = p;
                if(p.left != null)deq.offerLast(p.left);
                if(p.right != null)deq.offerLast(p.right);
            }
            for(int i = 0, j = len-1; i < j ; i ++, j --){
                if(arr[i].left == null && arr[j].right != null)return false;
                if(arr[i].left != null && arr[j].right == null)return false;
                if(arr[j].left == null && arr[i].right != null)return false;
                if(arr[j].left != null && arr[i].right == null)return false;
                if(arr[i].left != null && arr[j].right != null && arr[i].left.val != 
                    arr[j].right.val)return false;
                if(arr[j].left != null && arr[i].right != null && arr[j].left.val != 
                    arr[i].right.val)return false;
            }
        }
        return true;
    }
}

迭代版本二

/**
 * 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 boolean isSymmetric(TreeNode root) {
        Deque<TreeNode> deq = new LinkedList<>();
        if(root == null)return true;
        deq.offerLast(root.left);
        deq.offerLast(root.right);
        while(!deq.isEmpty()){
            TreeNode childLeft = deq.pollFirst();
            TreeNode childRight = deq.pollFirst();
            if(childLeft == null && childRight == null)continue;
            if(childLeft != null && childRight == null)return false;
            if(childLeft == null && childRight != null)return false;
            if(childLeft != null && childRight != null && childLeft.val != childRight.val)return false;
            deq.offerLast(childLeft.left);
            deq.offerLast(childRight.right);
            deq.offerLast(childLeft.right);
            deq.offerLast(childRight.left);
        }
        return true;
    }
}

递归

/**
 * 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 boolean isSymmetric(TreeNode root) {
        return compareTree(root.left, root.right);
    }
    public boolean compareTree(TreeNode childL, TreeNode childR){
        if(childL == null && childR != null)return false;
        if(childL != null && childR == null)return false;
        if(childL == null && childR == null)return true;
        if(childL.val != childR.val)return false;
        boolean boolL = compareTree(childL.left, childR.right);
        boolean boooR = compareTree(childL.right, childR.left);
        return boolL && boooR;
    }
}

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