代码随想录 day 15 层序遍历 102 | 226.翻转二叉树 | 101.对称二叉树 2
/* // 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) { Queue<Node> queue = new LinkedList<>(); if (root != null) { queue.offer(root); } while (!queue.isEmpty()) { int size = queue.size(); Node node = null; Node nodePre = null; for (int i = 0; i < size; i++ ){ if (i == 0) { nodePre = queue.poll(); node = nodePre; } else { node = queue.poll(); nodePre.next = node; nodePre = nodePre.next; } if (node.left != null) { queue.offer(node.left); } if (node.right != null) { queue.offer(node.right); } } nodePre.next = null; } return root; } }
102. 二叉树的层序遍历
给你二叉树的根节点 root
,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)
输入:root = [3,9,20,null,null,15,7] 输出:[[3],[9,20],[15,7]]
算法:利用queue记录每个层的元素
/** * 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<>(); if (root == null) return res; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { int size = queue.size(); List<Integer> level = new ArrayList<>(); while(size-- > 0) { TreeNode cur = queue.poll(); level.add(cur.val); if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } res.add(new ArrayList<>(level)); } 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<>(); if (root == null) return res; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { List<Integer> level = new ArrayList<>(); int size = queue.size(); while (size-- > 0) { TreeNode cur = queue.poll(); level.add(cur.val); if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } res.add(new ArrayList<>(level)); } Collections.reverse(res); return res; } }
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> list = new ArrayList<>(); Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); if (root == null) { return list; } while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0;i < size; i++) { TreeNode cur = queue.poll(); if (i == size - 1) { list.add(cur.val); } if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } } return list; } }
/** * 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<>(); Queue<TreeNode> queue = new LinkedList<>(); if (root == null) return res; queue.offer(root); while (!queue.isEmpty()) { int size = queue.size(); double sum = 0.0; for (int i = 0; i < size; i++) { TreeNode cur = queue.poll(); sum += cur.val; if (cur.left != null) queue.offer(cur.left); if (cur.right != null) queue.offer(cur.right); } res.add(sum / size); } return res; } }
429. N 叉树的层序遍历
class Solution { public List<List<Integer>> levelOrder(Node root) { List<List<Integer>> res = new ArrayList<>(); if (root == null) return res; Queue<Node> queue = new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { int size = queue.size(); List<Integer> list = new ArrayList<>(); for (int i = 0; i < size; i++){ Node node = queue.poll(); list.add(node.val); List<Node> children = node.children; if (children == null || children.size() == 0) { continue; } for (Node child : children) { if (child != null) { queue.offer(child); } } } res.add(new ArrayList<>(list)); } return res; } }
515. 在每个树行中找最大值
class Solution { public List<Integer> largestValues(TreeNode root) { List<Integer> res= new ArrayList<>(); if (root == null) return res; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while(!queue.isEmpty()) { int max = Integer.MIN_VALUE; int size = queue.size(); for (int i = 0;i < size; i++) { TreeNode node = queue.poll(); max = Math.max(node.val, max); if (node.left != null) { queue.offer(node.left); } if (node.right != null) { queue.offer(node.right); } } res.add(max); } return res; } }
116. 填充每个节点的下一个右侧节点指针
class Solution { public Node connect(Node root) { Queue<Node> queue = new LinkedList<>(); if (root != null) { queue.offer(root); } while (!queue.isEmpty()) { int size = queue.size(); Node cur = queue.poll(); if (cur.left != null) queue.offer(cur.left); if (cur.right != null) queue.offer(cur.right); for (int i = 1; i < size; i++) { Node next = queue.poll(); if (next.left != null) queue.offer(next.left); if (next.right != null) queue.offer(next.right); cur.next = next; cur = next; } } 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) { Queue<Node> queue = new LinkedList<>(); if (root != null) { queue.offer(root); } while (!queue.isEmpty()) { int size = queue.size(); Node node = null; Node nodePre = null; for (int i = 0; i < size; i++ ){ if (i == 0) { nodePre = queue.poll(); node = nodePre; } else { node = queue.poll(); nodePre.next = node; nodePre = nodePre.next; } if (node.left != null) { queue.offer(node.left); } if (node.right != null) { queue.offer(node.right); } } nodePre.next = null; } return root; } }
104. 二叉树的最大深度
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); int dep = 0; while (!queue.isEmpty()) { int len = queue.size(); while (len > 0) { TreeNode node = queue.poll(); if (node.left != null) queue.offer(node.left); if (node.right != null) queue.offer(node.right); len--; } dep++; } return dep; } }
226. 翻转二叉树
给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
输入:root = [4,2,7,1,3,6,9] 输出:[4,7,2,9,6,3,1]
class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) {return null;} Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { int size = queue.size(); while (size-- > 0) { TreeNode node = queue.poll(); TreeNode tmp = node.left; node.left = node.right; node.right = tmp; if (node.left != null) queue.offer(node.left); if (node.right != null) queue.offer(node.right); } } return root; } }
101. 对称二叉树
class Solution { public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); queue.offer(root); while (!queue.isEmpty()) { TreeNode t1 = queue.poll(); TreeNode t2 = queue.poll(); if (t1 == null && t2 == null) continue; if (t1 == null || t2 == null) return false; if (t1.val != t2.val) return false; queue.offer(t1.left); queue.offer(t2.right); queue.offer(t2.left); queue.offer(t1.right); } return true; } }