Skip to content

Commit

Permalink
update algo_na
Browse files Browse the repository at this point in the history
  • Loading branch information
no5ix committed Sep 5, 2024
1 parent 6d44cb4 commit 0e0c455
Show file tree
Hide file tree
Showing 3 changed files with 303 additions and 28 deletions.
141 changes: 140 additions & 1 deletion source/_posts/algo_na.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,143 @@ class Solution {
}
}
}
```
```


# 二叉树

## 前序


## 中序

``` java
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left; //
} else {
cur = stack.pop();
result.add(cur.val); //
cur = cur.right; //
}
}
return result;
}
}
```

## 后序

## 层序

``` java
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> resultList = new ArrayList<List<Integer>>();
if (root == null ) {
return resultList;
}
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--;
}
resultList.add(itemList);
}

return resultList;
}
}
```

## 高度

- 二叉树节点的高度:指从`该节点`到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)
- 二叉树节点的深度:指从`根节点`到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
- 而根节点的高度就是二叉树的最大深度

## 深度

- 二叉树的深度: 根节点到最远叶子节点的最长路径上的节点数。
- 叶子节点: 是指没有子节点的节点。

### 最大深度

使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。
在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度,

``` java
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int depth = 0;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
while (!que.isEmpty()) {
int len = que.size();
depth++;
while (len > 0) {
TreeNode tmpNode = que.poll();
if (tmpNode.left != null) { que.offer(tmpNode.left); }
if (tmpNode.right != null) { que.offer(tmpNode.right); }
len--;
}
}
return depth;
}
}
```

### 最小深度

最小深度: 是从根节点到最近叶子节点的最短路径上的节点数量。

``` java
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int depth = 0;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
while (!que.isEmpty()) {
int len = que.size();
depth++;
while (len > 0) {
TreeNode tmpNode = que.poll();
if (tmpNode.left == null && tmpNode.right == null) {
// 当左右孩子都为空的时候,说明是最低点的一层了,退出
return depth;
}
if (tmpNode.left != null) { que.offer(tmpNode.left); }
if (tmpNode.right != null) { que.offer(tmpNode.right); }
len--;
}
}
return depth;
}
}
```

190 changes: 163 additions & 27 deletions source/code/test_algo_na.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import java.util.HashMap;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;
import java.util.Stack;
import java.util.Collections;
import java.util.Queue;

// class Solution { // lc704
// public int search(int[] numbers, int targetNumber) {
Expand Down Expand Up @@ -326,31 +331,162 @@ int peek() {
}
}

// class Solution {
// public int[] maxSlidingWindow(int[] nums, int k) {
// if (nums.length == 1) {
// return nums;
// }
// int len = nums.length - k + 1;
// //存放结果元素的数组
// int[] res = new int[len];
// int num = 0;
// //自定义队列
// MyQueue myQueue = new MyQueue();
// //先将前k的元素放入队列
// for (int i = 0; i < k; i++) {
// myQueue.add(nums[i]);
// }
// res[num++] = myQueue.peek();
// for (int i = k; i < nums.length; i++) {
// //滑动窗口移除最前面的元素,移除是判断该元素是否放入队列
// myQueue.poll(nums[i - k]);
// //滑动窗口加入最后面的元素
// myQueue.add(nums[i]);
// //记录对应的最大值
// res[num++] = myQueue.peek();
// }
// return res;
// }
// }

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> preorderTraversal(TreeNode root) {
// List<Integer> result = new ArrayList<>();
// if (root == null) {
// return result;
// }
// Stack<TreeNode> stack = new Stack<>();
// stack.push(root);
// while (!stack.isEmpty()) {
// TreeNode node = stack.pop();
// result.add(node.val);
// if (node.right != null) {
// stack.push(node.right);
// }
// if (node.left != null) {
// stack.push(node.left);
// }
// }
// return result;
// }
// }

// class Solution {
// public List<Integer> postorderTraversal(TreeNode root) {
// List<Integer> result = new ArrayList<>();
// if (root == null) {
// return result;
// }
// Stack<TreeNode> stack = new Stack<>();
// stack.push(root);
// while (!stack.isEmpty()) {
// TreeNode node = stack.pop();
// result.add(node.val);
// if (node.left != null) {
// stack.push(node.left);
// }
// if (node.right != null) {
// stack.push(node.right);
// }
// }
// Collections.reverse(result);
// return result;
// }
// }

// class Solution {
// public List<Integer> inorderTraversal(TreeNode root) {
// List<Integer> result = new ArrayList<>();
// if (root == null) {
// return result;
// }
// Stack<TreeNode> stack = new Stack<>();
// TreeNode cur = root;
// while (cur != null || !stack.isEmpty()) {
// if (cur != null) {
// stack.push(cur);
// cur = cur.left; // 左
// } else {
// cur = stack.pop();
// result.add(cur.val); // 中
// cur = cur.right; // 右
// }
// }
// return result;
// }
// }

// class Solution {
// public List<List<Integer>> levelOrder(TreeNode root) {
// List<List<Integer>> resultList = new ArrayList<List<Integer>>();
// if (root == null ) {
// return resultList;
// }
// 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--;
// }
// resultList.add(itemList);
// }

// return resultList;
// }
// }

class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if (nums.length == 1) {
return nums;
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int len = nums.length - k + 1;
//存放结果元素的数组
int[] res = new int[len];
int num = 0;
//自定义队列
MyQueue myQueue = new MyQueue();
//先将前k的元素放入队列
for (int i = 0; i < k; i++) {
myQueue.add(nums[i]);
int depth = 0;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
while (!que.isEmpty()) {
int len = que.size();
depth++;
while (len > 0) {
TreeNode tmpNode = que.poll();
if (tmpNode.left != null) { que.offer(tmpNode.left); }
if (tmpNode.right != null) { que.offer(tmpNode.right); }
len--;
}
}
res[num++] = myQueue.peek();
for (int i = k; i < nums.length; i++) {
//滑动窗口移除最前面的元素,移除是判断该元素是否放入队列
myQueue.poll(nums[i - k]);
//滑动窗口加入最后面的元素
myQueue.add(nums[i]);
//记录对应的最大值
res[num++] = myQueue.peek();
}
return res;
return depth;
}
}

Expand Down Expand Up @@ -382,10 +518,10 @@ public static void main(String[] args){
// int[] ret = solution.intersection(myList, myList2);


int[] myList = {1,3,-1,-3,5,3,6,7};
int[] ret = solution.maxSlidingWindow(myList, 3);
for (int j = 0; j < ret.length; ++j) {
System.out.println(ret[j]);
}
// int[] myList = {1,3,-1,-3,5,3,6,7};
// int[] ret = solution.maxSlidingWindow(myList, 3);
// for (int j = 0; j < ret.length; ++j) {
// System.out.println(ret[j]);
// }
}
}
Binary file added source/pdf/eng_clause/eng_clause.pdf
Binary file not shown.

0 comments on commit 0e0c455

Please sign in to comment.