此篇后续会一直慢慢更新完所有题目!!!
102.二叉树的层序遍历
题目链接:https://leetcode.cn/problems/binary-tree-level-order-traversal/submissions/
class Solution {
public List<List<Integer>> reslist=new ArrayList<List<Integer>>();
public List<List<Integer>> levelOrder(TreeNode root) {
checkFun(root);
return reslist;
}
public void checkFun(TreeNode node){
if(node==null){
return;
}
//设置队列存放节点,集合存放节点对应的数值
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.offer(node);
while(!queue.isEmpty()){
List<Integer> list=new ArrayList<Integer>();
int len=queue.size();
while(len>0){
TreeNode tempNode=queue.poll();
list.add(tempNode.val);
if(tempNode.left!=null){
queue.offer(tempNode.left);
}
if(tempNode.right!=null){
queue.offer(tempNode.right);
}
//每执行完一次将节点从队列出来,那么队列长度就会减少1
len--;
}
reslist.add(list);
}
}
}
学习此题后,还可以完成下列中的九道题目:
107.二叉树的层次遍历2
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
List<List<Integer>> res1=new ArrayList<List<Integer>>();
if(root==null){
return res;
}
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
List<Integer> list=new ArrayList<Integer>();
int len=queue.size();
while(len>0){
TreeNode temnode=queue.poll();
list.add(temnode.val);
if(temnode.left!=null){
queue.offer(temnode.left);
}
if(temnode.right!=null){
queue.offer(temnode.right);
}
len--;
}
res.add(list);
}
for(int i=res.size()-1;i>=0;i--){
res1.add(res.get(i));
}
return res1;
}
}
199.二叉树的右视图
//只能看到每一层的最右边,相当于在遍历树时,将每一层最后一个加入到集合中
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> list=new ArrayList<Integer>();
Queue<TreeNode> queue=new LinkedList<TreeNode>();
if(root==null){
return list;
}
queue.offer(root);
while(!queue.isEmpty()){
int len=queue.size();
for(int i=0;i<len;i++){
TreeNode node=queue.poll();
if(node.left!=null){
queue.offer(node.left);
}
if(node.right!=null){
queue.offer(node.right);
}
if(i==len-1){
list.add(node.val);
}
}
}
return list;
}
}
637.二叉树的平均值
429.N叉树的层序遍历
难点:本题与二叉树的层次遍历不同的地方是:二叉树只有两个孩子,所以每次只要加入节点对应的左右孩子就好,但是N叉树有n个孩子,所以这里要利用队列的addAll()方法将所有的孩子加入队列中,其他的都是相同的。
class Solution {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
if(root==null){
return res;
}
Queue<Node> queue=new LinkedList<Node>();
queue.offer(root);
while(!queue.isEmpty()){
int len=queue.size();
List<Integer> list=new ArrayList<Integer>();
for(int i=0;i<len;i++){
Node node=queue.poll();
list.add(node.val);
//难点:如何把全部的孩子都加入到队列中,利用addAll()将所有孩子节点值都添加
if(node.children!=null){
queue.addAll(node.children);
}
}
res.add(list);
}
return res;
}
}
515.在每个树行中找最大值
116.填充每个节点的下一个右侧指针
117.填充每个节点的下一个右侧指针2
104.二叉树的最大深度
难点:按照自己的理解来解释:
1.高度:像楼层一样越来越高,所以是从下往上高的;因此是后序遍历(左右中):孩子将已经统计到的高度反馈给中,一直重复往上。
2.深度:像到海底一样的深度越来越深,所以是从上往下深的;因此是前序遍历(中左右):同上道理。
本题关键:求最大深度,也就是二叉树的最大高度,所以使用前序或者后续都是可以实现本题的。
可以使用递归法,也可以按照层序遍历的迭代法;
//层序遍历,迭代法
class Solution {
public int maxDepth(TreeNode root) {
int depth=0;
if(root==null){
return 0;
}
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
int len=queue.size();
for(int i=0;i<len;i++){
TreeNode node=queue.poll();
if(node.left!=null){
queue.offer(node.left);
}
if(node.right!=null){
queue.offer(node.right);
}
}
depth++;
}
return depth;
}
}
//递归法:求深度,从上到下,用前序遍历
//每次获得左右子树中的最大深度,随着层数往下,那么深度加1;
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
//求最大深度,从根节点开始
return getDepth(root,0);
}
public int getDepth(TreeNode node,int level){
//因为最大深度也是最大高度,所以用前序或者后续都是可以的
if(node==null){
return level;
}
level++;
int leftDepth=getDepth(node.left,level);
int rightDepth=getDepth(node.right,level);
return Math.max(leftDepth,rightDepth);
}
}
与本题相关的题目还有:
559.N叉树的最大深度
可以用迭代法(层序遍历)或者递归法(前、后序遍历)
//层序遍历获得最大深度
class Solution {
public int maxDepth(Node root) {
int depth=0;
Queue<Node> queue=new LinkedList<Node>();
if(root==null){
return 0;
}
queue.offer(root);
while(!queue.isEmpty()){
int len=queue.size();
for(int i=0;i<len;i++){
Node node=queue.poll();
if(node.children!=null){
queue.addAll(node.children);
}
}
depth++;
}
return depth;
}
}
递归是我个人的一大难点,迭代法就在模板基础上改变就好,但是递归真的不知道什么时候该调用自己方法递归!
111.二叉树的最小深度
226.翻转二叉树
题目链接:https://leetcode.cn/problems/invert-binary-tree/submissions/
思路:可以用递归或者迭代法,其中的原理都是:获得一个节点的左右孩子,然后用第三方变量来对这个左右孩子进行交换即为反转。
//深度优先遍历
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return null;
}
//前序和后续遍历都可以
swap(root);
invertTree(root.left);
invertTree(root.right);
// swap(root);
return root;
}
public void swap(TreeNode node){
TreeNode temp;
temp=node.left;
node.left=node.right;
node.right=temp;
}
}
//广度优先遍历
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return null;
}
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
int len=queue.size();
while(len>0){
TreeNode temNode=queue.poll();
swap(temNode);
if(temNode.left!=null){
queue.offer(temNode.left);
}
if(temNode.right!=null){
queue.offer(temNode.right);
}
len--;
}
}
return root;
}
public void swap(TreeNode node){
TreeNode twmp;
twmp=node.left;
node.left=node.right;
node.right=twmp;
}
}