leecode更新

This commit is contained in:
markilue 2023-01-08 13:02:04 +08:00
parent 2309bfa13d
commit cf77fa5d9b
18 changed files with 342 additions and 14 deletions

View File

@ -16,7 +16,7 @@ import java.util.Queue;
* @Version: 1.0
*/
public class InvertTree {
public class T05_InvertTree {
@Test

View File

@ -16,7 +16,7 @@ import java.util.Queue;
* 给你一个二叉树的根节点 root 检查它是否轴对称
* @Version: 1.0
*/
public class IsSymmetric {
public class T06_IsSymmetric {
@Test
public void test() {

View File

@ -16,7 +16,7 @@ import java.util.*;
* 给你二叉树的根节点 root 返回它节点值的 前序 遍历
*@Version: 1.0
*/
public class T01_PreOrderTraversal {
public class T01_0_PreOrderTraversal {
@Test
public void test() {

View File

@ -0,0 +1,71 @@
package com.markilue.leecode.tree.second;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.tree.second
*@Author: dingjiawen
*@CreateTime: 2023-01-08 11:36
*@Description:
* TODO 力扣589题 N叉树的前序遍历:
* 给定一个 n 叉树的根节点 root 返回 其节点值的 前序遍历
* n 叉树 在输入中按层序遍历进行序列化表示每组子节点由空值 null 分隔请参见示例
*@Version: 1.0
*/
public class T01_1_Preorder {
/**
* 思路:与前序遍历一致,只是从遍历left和right变成了children迭代法
* 速度击败17.14%内存击败39.29%
* @param root
* @return
*/
public List<Integer> preorder(NNode root) {
List<Integer> result = new ArrayList<>();
if(root==null){
return result;
}
Stack<NNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()){
NNode node = stack.pop();
result.add(node.val);
for (int i = node.children.size()-1; i >=0 ; i--) {
NNode child = node.children.get(i);
if(child!=null){
stack.push(child);
}
}
}
return result;
}
/**
* 思路:递归法
* 速度击败100%内存击败17.14%
* @param root
* @return
*/
List<Integer> result = new ArrayList<>();
public List<Integer> preorder1(NNode root) {
travel(root);
return result;
}
public void travel(NNode root) {
if(root==null){
return;
}
result.add(root.val);
for (NNode child : root.children) {
travel(child);
}
}
}

View File

@ -16,7 +16,7 @@ import java.util.*;
* 给你一棵二叉树的根节点 root 返回其节点值的 后序遍历
*@Version: 1.0
*/
public class T02_PostOrderTraversal {
public class T02_0_PostOrderTraversal {
@Test
public void test() {

View File

@ -0,0 +1,76 @@
package com.markilue.leecode.tree.second;
import com.markilue.leecode.tree.TreeNode;
import com.markilue.leecode.tree.TreeUtils;
import org.junit.Test;
import java.util.*;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.tree.second
*@Author: dingjiawen
*@CreateTime: 2023-01-05 10:25
*@Description:
* TODO 二刷leecode145题 二叉树的后序遍历:
* 给你一棵二叉树的根节点 root 返回其节点值的 后序遍历
*@Version: 1.0
*/
public class T02_1_Postorder {
/**
* 思路:递归法
* 速度击败100%内存击败49.35%
* @param root
* @return
*/
List<Integer> result;
public List<Integer> postorderTraversal(NNode root) {
result=new ArrayList<>();
postTraversal(root);
return result;
}
public void postTraversal(NNode root) {
if(root==null){
return;
}
for (NNode child : root.children) {
postTraversal(child);
}
result.add(root.val);
}
/**
* 思路:stack迭代法:中后前>反序前后中
* 速度击败26.37% 内存击败50.42%
* @param root
* @return
*/
public List<Integer> postorderTraversal1(NNode root) {
List<Integer> result = new ArrayList<>();
if(root==null){
return result;
}
Stack<NNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()){
NNode node = stack.pop();
result.add(node.val);
for (NNode child : node.children) {
stack.push(child);
}
}
Collections.reverse(result);
return result;
}
}

View File

@ -16,7 +16,7 @@ import java.util.*;
* 给你二叉树的根节点 root 返回其节点值的 层序遍历 即逐层地从左到右访问所有节点
*@Version: 1.0
*/
public class T04_LevelOrder {
public class T04_0_LevelOrder {
@Test
public void test() {

View File

@ -17,7 +17,7 @@ import java.util.*;
* 即按从叶子节点所在层到根节点所在的层逐层从左向右遍历
*@Version: 1.0
*/
public class T041_LevelOrderBottom {
public class T04_1_LevelOrderBottom {
@Test
public void test() {

View File

@ -17,7 +17,7 @@ import java.util.Queue;
* 给定一个二叉树的 根节点 root想象自己站在它的右侧按照从顶部到底部的顺序返回从右侧所能看到的节点值
*@Version: 1.0
*/
public class T042_RightSideView {
public class T04_2_RightSideView {
/**
* 思路就是返回他所有最右侧的节点层序遍历确实可以解决返回每一层最后的节点

View File

@ -16,7 +16,7 @@ import java.util.*;
* 给定一个非空二叉树的根节点 root , 以数组的形式返回每一层节点的平均值与实际答案相差 10-5 以内的答案可以被接受
*@Version: 1.0
*/
public class T043_averageOfLevels {
public class T04_3_averageOfLevels {
@Test
public void test() {

View File

@ -16,7 +16,7 @@ import java.util.Queue;
* 树的序列化输入是用层序遍历每组子节点都由 null 值分隔参见示例
*@Version: 1.0
*/
public class T044_LevelOrder {
public class T04_4_LevelOrder {
/**
* 思路N叉树的层序遍历本质上和二叉树无异BFS层序遍历版

View File

@ -17,7 +17,7 @@ import java.util.Queue;
* 给定一棵二叉树的根节点 root 请找出该二叉树中每一层的最大值
*@Version: 1.0
*/
public class T045_LargestValues {
public class T04_5_LargestValues {
/**

View File

@ -24,7 +24,7 @@ import java.util.List;
* 初始状态下所有 next 指针都被设置为 NULL
*@Version: 1.0
*/
public class T046_Connect {
public class T04_6_Connect {
/**

View File

@ -24,7 +24,7 @@ import java.util.List;
* 与T46似乎完全一致,所以代码完全没有改动
*@Version: 1.0
*/
public class T047_Connect {
public class T04_7_Connect {
/**

View File

@ -17,7 +17,7 @@ import java.util.Queue;
* 说明: 叶子节点是指没有子节点的节点
*@Version: 1.0
*/
public class T048_MaxDepth {
public class T04_8_MaxDepth {
/**
* 思路:深度本质上就是要遍历到最底层

View File

@ -21,7 +21,7 @@ import java.util.Queue;
* 说明叶子节点是指没有子节点的节点
*@Version: 1.0
*/
public class T049_MinDepth {
public class T04_9_MinDepth {
@Test
public void test() {

View File

@ -0,0 +1,67 @@
package com.markilue.leecode.tree.second;
import com.markilue.leecode.tree.TreeNode;
import java.util.Stack;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.tree.second
*@Author: dingjiawen
*@CreateTime: 2023-01-08 11:04
*@Description:
* TODO 翻转二叉树:
* 给你一棵二叉树的根节点 root 翻转这棵二叉树并返回其根节点
*@Version: 1.0
*/
public class T05_InvertTree {
/**
* 思路:递归法
* 速度击败100%内存击败5.28%
* @param root
* @return
*/
public TreeNode invertTree(TreeNode root) {
if(root==null||(root.left==null&&root.right==null)){
return root;
}
TreeNode temp=root.left;
root.left=invertTree(root.right);
root.right=invertTree(temp);
return root;
}
/**
* 思路:迭代法前序遍历;层序遍历也可
* 速度击败100%内存击败5.28%
* @param root
* @return
*/
public TreeNode invertTree1(TreeNode root) {
if(root==null||(root.left==null&&root.right==null)){
return root;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur=root;
while (cur!=null||!stack.isEmpty()){
if(cur!=null){
if(root.left!=null||root.right!=null){
TreeNode temp=cur.left;
cur.left=cur.right;
cur.right=temp;
}
stack.push(cur);
cur=cur.left;
}else {
TreeNode node = stack.pop();
cur=node.right;
}
}
return root;
}
}

View File

@ -0,0 +1,114 @@
package com.markilue.leecode.tree.second;
import com.markilue.leecode.tree.TreeNode;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
/**
*@BelongsProject: Leecode
*@BelongsPackage: com.markilue.leecode.tree.second
*@Author: dingjiawen
*@CreateTime: 2023-01-08 12:23
*@Description:
* TODO 二刷力扣101 对称二叉树:
* 给你一个二叉树的根节点 root 检查它是否轴对称
*@Version: 1.0
*/
public class T06_IsSymmetric {
/**
* 递归法
* 速度击败100%内存击败26.75%
* @param root
* @return
*/
public boolean isSymmetric(TreeNode root) {
if (root.left == null && root.right == null) {
return true;
} else {
return find(root.left, root.right);
}
}
public boolean find(TreeNode node1, TreeNode node2) {
if (node1 == null && node2 == null) {
return true;
} else {
if (node1 != null && node2 != null && node1.val == node2.val) {
return find(node1.left, node2.right) && find(node1.right, node2.left);
} else {
return false;
}
}
}
/**
* 迭代法理论上来说只能层序遍历,需要频繁地队列出入栈比较慢
* 速度击败22.81%内存击败59.3%
* @param root
* @return
*/
public boolean isSymmetric1(TreeNode root) {
Deque<TreeNode> queue = new LinkedList<>();
queue.offer(root.left);
queue.offer(root.right);
while (!queue.isEmpty()){
TreeNode node1 = queue.removeFirst();
TreeNode node2 = queue.removeLast();
if((node1==null&&node2==null)){
continue;
}
if(node1!=null&&node2!=null&&node1.val==node2.val){
queue.addFirst(node1.left);
queue.addLast(node2.right);
queue.addFirst(node1.right);
queue.addLast(node2.left);
}else{
return false;
}
}
return true;
}
/**
* 官方的迭代法:事实上不需要双端队列加在头尾只需要成对取即可
* @param root
* @return
*/
public boolean isSymmetric2(TreeNode root) {
return check(root, root);
}
public boolean check(TreeNode u, TreeNode v) {
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(u);
q.offer(v);
while (!q.isEmpty()) {
u = q.poll();
v = q.poll();
if (u == null && v == null) {
continue;
}
if ((u == null || v == null) || (u.val != v.val)) {
return false;
}
q.offer(u.left);
q.offer(v.right);
q.offer(u.right);
q.offer(v.left);
}
return true;
}
}