代码随想录训练营第15天| 层序遍历(我要打十个)、226.翻转二叉树、101.对称二叉树

层序遍历(我要打十个) 

102.二叉树的层序遍历

题目链接:102. 二叉树的层序遍历 - 力扣(LeetCode)

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Deque<TreeNode> dq = new LinkedList<>();
        List<List<Integer>> ans = new ArrayList<>();
        if (root == null) {
            return ans;
        }
        dq.addLast(root);
        int count1 = 1;
        while (!dq.isEmpty()) {
            int count = count1;
            count1 = 0;
            List<Integer> list = new ArrayList<>();
            while (count != 0) {
                TreeNode a = dq.pollFirst();
                if (a.left != null) {
                    dq.offerLast(a.left);
                    ++count1;
                }
                if (a.right != null) {
                    dq.offerLast(a.right);
                    ++count1;
                }
                list.add(a.val);
                --count;
            }
            ans.add(list);
        }
        return ans;
    }
}

 107.二叉树的层序遍历||

题目链接:107. 二叉树的层序遍历 II - 力扣(LeetCode)

class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        Deque<TreeNode> dq = new LinkedList<>();
        List<List<Integer>> ans = new ArrayList<>();
        if (root == null) {
            return ans;
        }
        dq.addLast(root);
        int count1 = 1;
        while (!dq.isEmpty()) {
            int count = count1;
            count1 = 0;
            List<Integer> list = new ArrayList<>();
            while (count != 0) {
                TreeNode a = dq.pollFirst();
                if (a.left != null) {
                    dq.offerLast(a.left);
                    ++count1;
                }
                if (a.right != null) {
                    dq.offerLast(a.right);
                    ++count1;
                }
                list.add(a.val);
                --count;
            }
            ans.addFirst(list);
        }
        return ans;
    }
}

199.二叉树的右视图

题目链接:199. 二叉树的右视图 - 力扣(LeetCode)

class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Deque<TreeNode> dq = new LinkedList<>();
        if(root == null) {
            return list;
        }
        dq.offerLast(root);
        int count1 = 1;
        while(!dq.isEmpty()) {
            int count = count1;
            count1 = 0;
            TreeNode a = null;
            while(count != 0) {
                a = dq.pollFirst();
                if(a.left != null) {
                    dq.offerLast(a.left);
                    ++count1;
                }
                if(a.right != null) {
                    dq.offerLast(a.right);
                    ++count1;
                }
                --count; 
            }
            list.add(a.val);
        }
        return list;
    }
}

637.二叉树的层平均值

题目链接:637. 二叉树的层平均值 - 力扣(LeetCode)

class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> list = new ArrayList<>();
        Deque<TreeNode> dq = new LinkedList<>();
        if(root == null) {
            return list;
        }
        dq.add(root);
        int count1 = 1;
        while(!dq.isEmpty()) {
            int count = count1;
            int n = count1;
            count1 = 0;
            double sum = 0;
            while(count != 0) {
                TreeNode a = dq.pollFirst();
                sum += a.val;
                if(a.left != null) {
                    ++count1;
                    dq.offerLast(a.left);
                }
                if(a.right != null) {
                    ++count1;
                    dq.offerLast(a.right);
                }
                --count;
            }
            list.addLast(sum/n);
        }
        return list;
    }
}

429.N叉树的层序遍历

题目链接:429. N 叉树的层序遍历 - 力扣(LeetCode)

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> ans = new ArrayList<>();
        if(root == null) {
            return ans;
        }
        Deque<Node> dq1 = new LinkedList<>();
        List<Integer> c = new ArrayList<>();
        c.addLast(root.val);
        ans.addFirst(c);
        dq1.offerFirst(root);
        while(!dq1.isEmpty()) {
            Deque<Node> dq2 = new LinkedList<>();
            while(!dq1.isEmpty()) {
                dq2.offerLast(dq1.pollFirst());
            }
            List<Integer> list = new ArrayList<>(); 
            while(!dq2.isEmpty()) {
                Node a = dq2.pollFirst();
                for(Node b : a.children) {
                    if(b != null) {
                        list.addLast(b.val);
                        dq1.offerLast(b);
                    }
                }
            }
            if(!list.isEmpty())
            ans.addLast(list);
        }
        return ans;
    }
}

515.在每个树行中找最大值

题目链接:515. 在每个树行中找最大值 - 力扣(LeetCode)

class Solution {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        if(root == null) {
            return ans;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()) {
            int a = q.size();
            int max = Integer.MIN_VALUE;
            for(int i = 0; i < a; ++i) {
                TreeNode b = q.poll();
                max = b.val > max ? b.val : max;
                if(b.left != null) {
                    q.offer(b.left);
                }
                if(b.right != null) {
                    q.offer(b.right);
                }
            }
            ans.addLast(max);
        }
        return ans;
    }
}

116.填充每个节点的下一个右侧节点指针

题目链接:116. 填充每个节点的下一个右侧节点指针 - 力扣(LeetCode)

class Solution {
    public Node connect(Node root) {
        if(root == null || root.left == null) {
            return root;
        }
        Node a = root,b = a;
        while(b != null && b.left != null) {
            while(a != null) {
                a.left.next = a.right;
                a.right.next = (a.next == null) ? null : a.next.left;
                a = a.next;
            }
            b = b.left;
            a = b;
        }
        return root;
    }
}

 117.填充每个节点的下一个右侧节点指针

题目链接:117. 填充每个节点的下一个右侧节点指针 II - 力扣(LeetCode)

周末补上

104.二叉树的最大深度

题目链接:104. 二叉树的最大深度 - 力扣(LeetCode)

class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int high = 0;
        while(!q.isEmpty()) {
            int size = q.size();
            ++high;
            for(int i = 0; i < size; ++i) {
                TreeNode a = q.poll();
                if(a.left != null) {
                    q.offer(a.left);
                }
                if(a.right != null) {
                    q.offer(a.right);
                }
            }
        }
        return high;
    }
}

111.二叉树的最小深度

题目链接:111. 二叉树的最小深度 - 力扣(LeetCode)

class Solution {
    public int minDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int high = 1;
        while(!q.isEmpty()) {
            int size = q.size();
            for(int i = 0; i < size; ++i){
                TreeNode a = q.poll();
                if(a.left == null && a.right == null) {
                    return high;
                }
                if(a.left != null) {
                    q.offer(a.left);
                }
                if(a.right != null) {
                    q.offer(a.right);
                }
            }
            ++high;
        }
        return high;
    }
}

226.翻转二叉树

题目链接:226. 翻转二叉树 - 力扣(LeetCode)

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null ||(root.left == null && root.right == null)) {
            return root;
        }
        helper(root);
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    private void helper(TreeNode root) {
        TreeNode a = root.left;
        root.left = root.right;
        root.right = a;
    }
}

101.对称二叉树

题目链接:101. 对称二叉树 - 力扣(LeetCode)

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null) {
            return true;
        }else {
            return helper(root.left, root.right);
        }
    }
    private boolean helper(TreeNode left, TreeNode right) {
        if(left == null && right == null) {
            return true;
        }else if(left == null || right == null) {
            return false;
        }
        if(left.val != right.val) {
            return false;
        }
        return helper(left.left, right.right) && helper(left.right, right.left);        
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值