赞
踩
看完本篇可以一口气刷十道题,试一试, 层序遍历并不难,大家可以很快刷了十道题。
题目链接/文章讲解/视频讲解:代码随想录
- 102.二叉树的层序遍历(opens new window)
public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> result = new ArrayList<List<Integer>>(); if(root == null){ return result; } Queue<TreeNode> que = new LinkedList<TreeNode>(); que.offer(root); while(!que.isEmpty()){ List<Integer> itemList = new ArrayList<Integer>(); int len = que.size(); while (len > 0) { TreeNode tmpNode = que.poll(); itemList.add(tmpNode.val); if (tmpNode.left != null) que.offer(tmpNode.left); if (tmpNode.right != null) que.offer(tmpNode.right); len--; } result.add(itemList); } return result; }- 107.二叉树的层次遍历II(opens new window)
public List<List<Integer>> solution1(TreeNode root) { List<List<Integer>> list = new ArrayList<>(); Deque<TreeNode> que = new LinkedList<>(); if (root == null) { return list; } que.offerLast(root); while (!que.isEmpty()) { List<Integer> levelList = new ArrayList<>(); int levelSize = que.size(); for (int i = 0; i < levelSize; i++) { TreeNode peek = que.peekFirst(); levelList.add(que.pollFirst().val); if (peek.left != null) { que.offerLast(peek.left); } if (peek.right != null) { que.offerLast(peek.right); } } list.add(levelList); } List<List<Integer>> result = new ArrayList<>(); for (int i = list.size() - 1; i >= 0; i-- ) { result.add(list.get(i)); } return result; }- 199.二叉树的右视图(opens new window)
public List<Integer> rightSideView(TreeNode root) { List<Integer> levelList = new ArrayList<>(); if(root == null){ return levelList; } Queue<TreeNode> que = new LinkedList<TreeNode>(); int temp = 0; que.offer(root); while(!que.isEmpty()){ int len = que.size(); while(len>0){ TreeNode peek = que.poll(); int val = peek.val; temp = val; if (peek.left != null) { que.offer(peek.left); } if (peek.right != null) { que.offer(peek.right); } len--; if(len==0){ levelList.add(temp); } } } return levelList; }- 637. 二叉树的层平均值
public List<Double> averageOfLevels(TreeNode root) { List<Double> levelList = new ArrayList<>(); if(root == null){ return levelList; } Queue<TreeNode> que = new LinkedList<TreeNode>(); que.offer(root); while(!que.isEmpty()){ int len = que.size(); int len1 = len; double temp = 0.0; while(len>0){ TreeNode peek = que.poll(); int val = peek.val; temp += val; if (peek.left != null) { que.offer(peek.left); } if (peek.right != null) { que.offer(peek.right); } len--; if(len==0){ levelList.add(temp/len1); } } } return levelList; }- 429.N叉树的层序遍历(opens new window)
public List<List<Integer>> levelOrder(Node root) { List<List<Integer>> result = new ArrayList<List<Integer>>(); if(root == null){ return result; } Queue<Node> que = new LinkedList<Node>(); que.offer(root); while(!que.isEmpty()){ List<Integer> itemList = new ArrayList<Integer>(); int len = que.size(); while (len > 0) { Node tmpNode = que.poll(); itemList.add(tmpNode.val); for (int i = 0; i < tmpNode.children.size(); i++ ) { que.offer(tmpNode.children.get(i)); } len--; } result.add(itemList); } return result; }- 515.在每个树行中找最大值(opens new window)
public List<Integer> largestValues(TreeNode root) { List<Integer> levelList = new ArrayList<>(); if(root == null){ return levelList; } Queue<TreeNode> que = new LinkedList<TreeNode>(); que.offer(root); while(!que.isEmpty()){ int len = que.size(); int len1 = len; int temp = Integer.MIN_VALUE; while(len>0){ TreeNode peek = que.poll(); int val = peek.val; temp = Math.max(temp,val); if (peek.left != null) { que.offer(peek.left); } if (peek.right != null) { que.offer(peek.right); } len--; if(len==0){ levelList.add(temp); } } } return levelList; }- 116.填充每个节点的下一个右侧节点指针(opens new window)
public Node connect(Node root) { if(root == null){ return null; } Queue<Node> que = new LinkedList<Node>(); que.offer(root); while(!que.isEmpty()){ int len = que.size(); Node peek = que.poll(); if (peek.left != null) { que.offer(peek.left); } if (peek.right != null) { que.offer(peek.right); } for (int index = 1; index < len; index++){ Node next = que.poll(); if (next.left != null) { que.offer(next.left); } if (next.right != null) { que.offer(next.right); } peek.next = next; peek = next; } } return root; }- 117.填充每个节点的下一个右侧节点指针II(opens new window)
- 同上
- 104.二叉树的最大深度(opens new window)
public int maxDepth(TreeNode root) { if(root == null){ return 0; } int deep = Math.max(maxDepth(root.left),maxDepth(root.right)); return deep+1; }- 111.二叉树的最小深度
public int minDepth(TreeNode root) { if(root == null){ return 0; } if(root.left == null || root.right == null ){ return minDepth(root.left)+minDepth(root.right)+1; } return Math.min(minDepth(root.left),minDepth(root.right))+1; }
这道题目 一些做过的同学 理解的也不够深入,建议大家先看我的视频讲解,无论做过没做过,都会有很大收获。
文章讲解/视频讲解:代码随想录
226. 翻转二叉树题目链接:226. 翻转二叉树
public TreeNode invertTree(TreeNode root) { resever(root); return root; } public void resever(TreeNode root){ if(root == null){ return; } resever(root.right); resever(root.left); TreeNode temp = root.right; root.right =root.left; root.left = temp; }
先看视频讲解,会更容易一些。
题目链接:101. 对称二叉树
文章讲解/视频讲解:代码随想录
public boolean isSymmetric(TreeNode root) { return compare(root.left, root.right); } private boolean compare(TreeNode left, TreeNode right) { if (left == null && right != null) { return false; } if (left != null && right == null) { return false; } if (left == null && right == null) { return true; } if (left.val != right.val) { return false; } // 比较外侧 boolean compareOutside = compare(left.left, right.right); // 比较内侧 boolean compareInside = compare(left.right, right.left); return compareOutside && compareInside; }
拓展:
public boolean isSameTree(TreeNode left, TreeNode right) { if (left == null && right != null) { return false; } if (left != null && right == null) { return false; } if (left == null && right == null) { return true; } if (left.val != right.val) { return false; } return isSameTree(left.left, right.left) && isSameTree(left.right, right.right); }
public boolean isSubtree(TreeNode root, TreeNode subRoot) { if(root == null && subRoot == null){ return true; } if (root == null || subRoot == null) { return false; } boolean ans2 = isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot); return isSametree(root, subRoot) || ans2; } public boolean isSametree(TreeNode s, TreeNode t) { if(s == null && t == null) return true; if(s == null || t == null) return false; return s.val == t.val && isSametree(s.left,t.left) && isSametree(s.right,t.right); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。