【算法刷题day15】Leetcode 各种层序遍历、226,2024年最新字节跳动Linux运维三面凉凉

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注运维)
img

正文

            sum += node.val;
            if (node.left != null)  queue.add(node.left);
            if (node.right != null) queue.add(node.right);
        }
        res.add(sum / size);
    }
    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> levelOrder(Node root) {
List<List> res = new ArrayList<List>();
if (root == null)
return res;
Queue queue = new LinkedList();
queue.add(root);
while (!queue.isEmpty()){
int size = queue.size();
List item = new ArrayList();
for (int i = 0; i < size; i++){
Node node = queue.poll();
item.add(node.val);
for (Node n : node.children){
if (n != null)
queue.add(n);
}
}
res.add(item);
}
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 largestValues(TreeNode root) {
List res = new ArrayList();
if (root == null)
return res;
Queue queue = new LinkedList();
queue.add(root);
while (!queue.isEmpty()){
int size = queue.size();
int max = Integer.MIN_VALUE;
for (int i = 0; i < size; i++){
TreeNode node = queue.poll();
max = Math.max(max, node.val);
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
res.add(max);
}
return res;
}
}



[**116.填充每个节点的下一个右侧节点指针**]( )



[**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) {
if (root == null)
return root;
Queue queue = new LinkedList();
queue.add(root);
while (!queue.isEmpty()){
int size = queue.size();
Node pre = queue.poll();
if (pre.left != null) queue.add(pre.left);
if (pre.right != null) queue.add(pre.right);
for (int i = 1; i < size; i++){
Node cur = queue.poll();
if (cur.left != null) queue.add(cur.left);
if (cur.right != null) queue.add(cur.right);
pre.next = cur;
pre = cur;
}
}
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) {
int depth = 0;
if (root == null)
return depth;
Queue queue = new LinkedList();
queue.add(root);
while(!queue.isEmpty()){
int size = queue.size();
depth++;
for (int i = 0; i < size; i++){
TreeNode node = queue.poll();
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
}
return depth;
}
}



[**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) {
if (root == null)
return 0;
int minDepth = Integer.MAX_VALUE;
int depth = 0;
Queue queue = new LinkedList();
queue.add(root);
while(!queue.isEmpty()){
int size = queue.size();
depth++;
for (int i = 0; i < size; i++){
TreeNode node = queue.poll();
if (node.left == null && node.right == null)
minDepth = Math.min(minDepth, depth);
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
}
return minDepth==Integer.MAX_VALUE ? 0 : minDepth;
}
}

//优化,不需要全部遍历完,因为是一层一层找下去,所以第一个叶子就是最低的
/**
* 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) {
if (root == null)
return 0;
int depth = 0;
Queue queue = new LinkedList();
queue.add(root);
while(!queue.isEmpty()){
int size = queue.size();
depth++;
for (int i = 0; i < size; i++){
TreeNode node = queue.poll();
if (node.left == null && node.right == null)
return depth;
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
}
return depth;
}
}


#### 总结


暂无


### Leetcode 226.翻转二叉树



> 
> **题目:**[**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) {
if (root == null)
return root;
TreeNode tmp = invertTree(root.left);
root.left = invertTree(root.right);
root.right = tmp;
return root;
}
}

//更能体现前序遍历和后续遍历
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null)
return root;
invertTree(root.left);
invertTree(root.right);
swapChildren(root);
return root;
}
private void swapChildren(TreeNode node){
TreeNode tmp = node.left;
node.left = node.right;
node.right = tmp;
}
}

//层序遍历
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null)
return root;
Queue queue = new LinkedList();
queue.add(root);
while (!queue.isEmpty()){
int size = queue.size();
for (int i = 0; i < size; i++){
TreeNode node = queue.poll();
swapChildren(node);
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
}
return root;
}
private void swapChildren(TreeNode node){
TreeNode tmp = node.left;
node.left = node.right;
node.right = tmp;
}
}

//前序
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null)
return root;
Stack stack = new Stack();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
swapChildren(node);
if (node.left != null) stack.push(node.left);
if (node.right != null) stack.push(node.right);
}
return root;
}
private void swapChildren(TreeNode node){
TreeNode tmp = node.left;
node.left = node.right;
node.right = tmp;
}
}

//统一深度遍历
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null)
return root;
Stack stack = new Stack();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.peek();
if (node != null){
stack.pop();
if (node.left != null) stack.push(node.left);
if (node.right != null) stack.push(node.right);
stack.push(node);
stack.push(null);
}else{
stack.pop();
node = stack.pop();
swapChildren(node);
}
}
return root;
}
private void swapChildren(TreeNode node){
TreeNode tmp = node.left;
node.left = node.right;
node.right = tmp;
}
}


#### 总结


暂无


### Leetcode 101. 对称二叉树



> 
> **题目:**[**101. 对称二叉树**]( )  
>  **解析:**[**代码随想录解析**]( )
> 
> 
> 


#### 解题思路


遍历遍历遍历


#### 代码



//递归
/**

最后的话

最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

资料预览

给大家整理的视频资料:

给大家整理的电子书资料:

如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注运维)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

/**

最后的话

最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

资料预览

给大家整理的视频资料:

[外链图片转存中…(img-Cfq4ZGSZ-1713207349239)]

给大家整理的电子书资料:

[外链图片转存中…(img-eePD4cNo-1713207349239)]

如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注运维)
[外链图片转存中…(img-Z3GSFgIc-1713207349240)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 14
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值