mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -397,9 +397,9 @@
|
|||||||
* [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md)
|
* [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md)
|
||||||
* [图论:827.最大人工岛](./problems/0827.最大人工岛.md)
|
* [图论:827.最大人工岛](./problems/0827.最大人工岛.md)
|
||||||
* [图论:127. 单词接龙](./problems/0127.单词接龙.md)
|
* [图论:127. 单词接龙](./problems/0127.单词接龙.md)
|
||||||
* [图论:841.钥匙和房间](./problems/841.钥匙和房间)
|
* [图论:841.钥匙和房间](./problems/0841.钥匙和房间.md)
|
||||||
* [图论:463. 岛屿的周长](./problems/0463.岛屿的周长.md)
|
* [图论:463. 岛屿的周长](./problems/0463.岛屿的周长.md)
|
||||||
* [图论:并查集理论基础](./problems/)
|
* [图论:并查集理论基础](./problems/图论并查集理论基础.md)
|
||||||
* [图论:1971. 寻找图中是否存在路径](./problems/1971.寻找图中是否存在路径.md)
|
* [图论:1971. 寻找图中是否存在路径](./problems/1971.寻找图中是否存在路径.md)
|
||||||
* [图论:684.冗余连接](./problems/0684.冗余连接.md)
|
* [图论:684.冗余连接](./problems/0684.冗余连接.md)
|
||||||
* [图论:685.冗余连接II](./problems/0685.冗余连接II.md)
|
* [图论:685.冗余连接II](./problems/0685.冗余连接II.md)
|
||||||
|
@ -100,8 +100,8 @@ int main() {
|
|||||||
while (cin >> n >> m) {
|
while (cin >> n >> m) {
|
||||||
vector<int> dp(n + 1, 0);
|
vector<int> dp(n + 1, 0);
|
||||||
dp[0] = 1;
|
dp[0] = 1;
|
||||||
for (int i = 1; i <= n; i++) { // 遍历物品
|
for (int i = 1; i <= n; i++) { // 遍历背包
|
||||||
for (int j = 1; j <= m; j++) { // 遍历背包
|
for (int j = 1; j <= m; j++) { // 遍历物品
|
||||||
if (i - j >= 0) dp[i] += dp[i - j];
|
if (i - j >= 0) dp[i] += dp[i - j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -897,6 +897,53 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
// 递归
|
||||||
|
public bool IsSymmetric(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return true;
|
||||||
|
return Compare(root.left, root.right);
|
||||||
|
}
|
||||||
|
public bool Compare(TreeNode left, TreeNode right)
|
||||||
|
{
|
||||||
|
if(left == null && right != null) return false;
|
||||||
|
else if(left != null && right == null ) return false;
|
||||||
|
else if(left == null && right == null) return true;
|
||||||
|
else if(left.val != right.val) return false;
|
||||||
|
|
||||||
|
var outside = Compare(left.left, right.right);
|
||||||
|
var inside = Compare(left.right, right.left);
|
||||||
|
|
||||||
|
return outside&&inside;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
``` C#
|
||||||
|
// 迭代法
|
||||||
|
public bool IsSymmetric(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return true;
|
||||||
|
var st = new Stack<TreeNode>();
|
||||||
|
st.Push(root.left);
|
||||||
|
st.Push(root.right);
|
||||||
|
while (st.Count != 0)
|
||||||
|
{
|
||||||
|
var left = st.Pop();
|
||||||
|
var right = st.Pop();
|
||||||
|
if (left == null && right == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if ((left == null || right == null || (left.val != right.val)))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
st.Push(left.left);
|
||||||
|
st.Push(right.right);
|
||||||
|
st.Push(left.right);
|
||||||
|
st.Push(right.left);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -462,6 +462,32 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
public IList<IList<int>> LevelOrder(TreeNode root)
|
||||||
|
{
|
||||||
|
var res = new List<IList<int>>();
|
||||||
|
var que = new Queue<TreeNode>();
|
||||||
|
if (root == null) return res;
|
||||||
|
que.Enqueue(root);
|
||||||
|
while (que.Count != 0)
|
||||||
|
{
|
||||||
|
var size = que.Count;
|
||||||
|
var vec = new List<int>();
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
var cur = que.Dequeue();
|
||||||
|
vec.Add(cur.val);
|
||||||
|
if (cur.left != null) que.Enqueue(cur.left);
|
||||||
|
if (cur.right != null) que.Enqueue(cur.right);
|
||||||
|
}
|
||||||
|
res.Add(vec);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
|
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
|
||||||
@ -798,6 +824,31 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
public IList<IList<int>> LevelOrderBottom(TreeNode root)
|
||||||
|
{
|
||||||
|
var res = new List<IList<int>>();
|
||||||
|
var que = new Queue<TreeNode>();
|
||||||
|
if (root == null) return res;
|
||||||
|
que.Enqueue(root);
|
||||||
|
while (que.Count != 0)
|
||||||
|
{
|
||||||
|
var size = que.Count;
|
||||||
|
var vec = new List<int>();
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
var cur = que.Dequeue();
|
||||||
|
vec.Add(cur.val);
|
||||||
|
if (cur.left != null) que.Enqueue(cur.left);
|
||||||
|
if (cur.right != null) que.Enqueue(cur.right);
|
||||||
|
}
|
||||||
|
res.Add(vec);
|
||||||
|
}
|
||||||
|
res.Reverse();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## 199.二叉树的右视图
|
## 199.二叉树的右视图
|
||||||
|
|
||||||
|
@ -738,26 +738,36 @@ function maxDepth(root: TreeNode | null): number {
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// 后续遍历(自下而上)
|
// 后续遍历(自下而上)
|
||||||
function maxDepth(root: TreeNode | null): number {
|
function maxDepth(root: Node | null): number {
|
||||||
if (root === null) return 0;
|
if (root === null) return 0
|
||||||
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
|
let depth = 0
|
||||||
};
|
for (let i = 0; i < root.children.length; i++) {
|
||||||
|
depth = Math.max(depth, maxDepth(root.children[i]))
|
||||||
|
}
|
||||||
|
return depth + 1
|
||||||
|
}
|
||||||
|
|
||||||
// 前序遍历(自上而下)
|
// 前序遍历(自上而下)
|
||||||
function maxDepth(root: TreeNode | null): number {
|
function maxDepth(root: Node | null): number {
|
||||||
function recur(node: TreeNode | null, count: number) {
|
if (root === null) return 0
|
||||||
if (node === null) {
|
|
||||||
resMax = resMax > count ? resMax : count;
|
let depth: number = 0
|
||||||
return;
|
const queue: Array<Node | null> = []
|
||||||
|
queue.push(root)
|
||||||
|
|
||||||
|
while (queue.length > 0) {
|
||||||
|
let len = queue.length
|
||||||
|
depth++
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
// 当前层遍历
|
||||||
|
let curNode: Node | null = queue.shift()!
|
||||||
|
for (let j = 0; j < curNode.children.length; j++) {
|
||||||
|
if (curNode.children[j]) queue.push(curNode.children[j])
|
||||||
}
|
}
|
||||||
recur(node.left, count + 1);
|
|
||||||
recur(node.right, count + 1);
|
|
||||||
}
|
}
|
||||||
let resMax: number = 0;
|
}
|
||||||
let count: number = 0;
|
return depth
|
||||||
recur(root, count);
|
}
|
||||||
return resMax;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -1022,6 +1032,61 @@ impl Solution {
|
|||||||
max_depth
|
max_depth
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
// 递归法
|
||||||
|
public int MaxDepth(TreeNode root) {
|
||||||
|
if(root == null) return 0;
|
||||||
|
|
||||||
|
int leftDepth = MaxDepth(root.left);
|
||||||
|
int rightDepth = MaxDepth(root.right);
|
||||||
|
|
||||||
|
return 1 + Math.Max(leftDepth, rightDepth);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```C#
|
||||||
|
// 前序遍历
|
||||||
|
int result = 0;
|
||||||
|
public int MaxDepth(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return result;
|
||||||
|
GetDepth(root, 1);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public void GetDepth(TreeNode root, int depth)
|
||||||
|
{
|
||||||
|
result = depth > result ? depth : result;
|
||||||
|
if (root.left == null && root.right == null) return;
|
||||||
|
|
||||||
|
if (root.left != null)
|
||||||
|
GetDepth(root.left, depth + 1);
|
||||||
|
if (root.right != null)
|
||||||
|
GetDepth(root.right, depth + 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```C#
|
||||||
|
// 迭代法
|
||||||
|
public int MaxDepth(TreeNode root)
|
||||||
|
{
|
||||||
|
int depth = 0;
|
||||||
|
Queue<TreeNode> que = new();
|
||||||
|
if (root == null) return depth;
|
||||||
|
que.Enqueue(root);
|
||||||
|
while (que.Count != 0)
|
||||||
|
{
|
||||||
|
int size = que.Count;
|
||||||
|
depth++;
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
var node = que.Dequeue();
|
||||||
|
if (node.left != null) que.Enqueue(node.left);
|
||||||
|
if (node.right != null) que.Enqueue(node.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -908,6 +908,31 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
public bool IsBalanced(TreeNode root)
|
||||||
|
{
|
||||||
|
return GetHeight(root) == -1 ? false : true;
|
||||||
|
}
|
||||||
|
public int GetHeight(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return 0;
|
||||||
|
int left = GetHeight(root.left);
|
||||||
|
if (left == -1) return -1;
|
||||||
|
int right = GetHeight(root.right);
|
||||||
|
if (right == -1) return -1;
|
||||||
|
int res;
|
||||||
|
if (Math.Abs(left - right) > 1)
|
||||||
|
{
|
||||||
|
res = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res = 1 + Math.Max(left, right);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -708,6 +708,49 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
// 递归
|
||||||
|
public int MinDepth(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return 0;
|
||||||
|
int left = MinDepth(root.left);
|
||||||
|
int right = MinDepth(root.right);
|
||||||
|
if (root.left == null && root.right != null)
|
||||||
|
return 1+right;
|
||||||
|
else if(root.left!=null && root.right == null)
|
||||||
|
return 1+left;
|
||||||
|
|
||||||
|
int res = 1 + Math.Min(left, right);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```C#
|
||||||
|
// 迭代
|
||||||
|
public int MinDepth(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return 0;
|
||||||
|
int depth = 0;
|
||||||
|
var que = new Queue<TreeNode>();
|
||||||
|
que.Enqueue(root);
|
||||||
|
while (que.Count > 0)
|
||||||
|
{
|
||||||
|
int size = que.Count;
|
||||||
|
depth++;
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
var node = que.Dequeue();
|
||||||
|
if (node.left != null)
|
||||||
|
que.Enqueue(node.left);
|
||||||
|
if (node.right != null)
|
||||||
|
que.Enqueue(node.right);
|
||||||
|
if (node.left == null && node.right == null)
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -1511,6 +1511,17 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
// 0112.路径总和
|
||||||
|
// 递归
|
||||||
|
public bool HasPathSum(TreeNode root, int targetSum)
|
||||||
|
{
|
||||||
|
if (root == null) return false;
|
||||||
|
if (root.left == null && root.right == null && targetSum == root.val) return true;
|
||||||
|
return HasPathSum(root.left, targetSum - root.val) || HasPathSum(root.right, targetSum - root.val);
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
|
@ -867,6 +867,31 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
// 递归
|
||||||
|
public int CountNodes(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return 0;
|
||||||
|
var left = root.left;
|
||||||
|
var right = root.right;
|
||||||
|
int leftDepth = 0, rightDepth = 0;
|
||||||
|
while (left != null)
|
||||||
|
{
|
||||||
|
left = left.left;
|
||||||
|
leftDepth++;
|
||||||
|
}
|
||||||
|
while (right != null)
|
||||||
|
{
|
||||||
|
right = right.right;
|
||||||
|
rightDepth++;
|
||||||
|
}
|
||||||
|
if (leftDepth == rightDepth)
|
||||||
|
return (int)Math.Pow(2, leftDepth+1) - 1;
|
||||||
|
return CountNodes(root.left) + CountNodes(root.right) + 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -1019,24 +1019,12 @@ public class Solution {
|
|||||||
//迭代
|
//迭代
|
||||||
public class Solution {
|
public class Solution {
|
||||||
public TreeNode InvertTree(TreeNode root) {
|
public TreeNode InvertTree(TreeNode root) {
|
||||||
if (root == null) return null;
|
if(root == null) return root;
|
||||||
Stack<TreeNode> stack=new Stack<TreeNode>();
|
(root.left,root.right) = (root.right, root.left);
|
||||||
stack.Push(root);
|
InvertTree(root.left);
|
||||||
while(stack.Count>0)
|
InvertTree(root.right);
|
||||||
{
|
|
||||||
TreeNode node = stack.Pop();
|
|
||||||
swap(node);
|
|
||||||
if(node.right!=null) stack.Push(node.right);
|
|
||||||
if(node.left!=null) stack.Push(node.left);
|
|
||||||
}
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void swap(TreeNode node) {
|
|
||||||
TreeNode temp = node.left;
|
|
||||||
node.left = node.right;
|
|
||||||
node.right = temp;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -900,6 +900,43 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
public IList<string> BinaryTreePaths(TreeNode root)
|
||||||
|
{
|
||||||
|
List<int> path = new();
|
||||||
|
List<string> res = new();
|
||||||
|
if (root == null) return res;
|
||||||
|
Traversal(root, path, res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
public void Traversal(TreeNode node, List<int> path, List<string> res)
|
||||||
|
{
|
||||||
|
path.Add(node.val);
|
||||||
|
if (node.left == null && node.right == null)
|
||||||
|
{
|
||||||
|
string sPath = "";
|
||||||
|
for (int i = 0; i < path.Count - 1; i++)
|
||||||
|
{
|
||||||
|
sPath += path[i].ToString();
|
||||||
|
sPath += "->";
|
||||||
|
}
|
||||||
|
sPath += path[path.Count - 1].ToString();
|
||||||
|
res.Add(sPath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (node.left != null)
|
||||||
|
{
|
||||||
|
Traversal(node.left, path, res);
|
||||||
|
path.RemoveAt(path.Count-1);
|
||||||
|
}
|
||||||
|
if (node.right != null)
|
||||||
|
{
|
||||||
|
Traversal(node.right, path, res);
|
||||||
|
path.RemoveAt(path.Count-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -250,6 +250,20 @@ class Solution:
|
|||||||
s[:] = [s[i] for i in range(len(s) - 1, -1, -1)]
|
s[:] = [s[i] for i in range(len(s) - 1, -1, -1)]
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
(版本七) 使用reverse()
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def reverseString(self, s: List[str]) -> None:
|
||||||
|
"""
|
||||||
|
Do not return anything, modify s in-place instead.
|
||||||
|
"""
|
||||||
|
# 原地反转,无返回值
|
||||||
|
s.reverse()
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
### Go:
|
### Go:
|
||||||
|
|
||||||
```Go
|
```Go
|
||||||
|
@ -214,6 +214,19 @@ class Solution:
|
|||||||
return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote))
|
return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
(版本六)使用count(简单易懂)
|
||||||
|
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
|
||||||
|
for char in ransomNote:
|
||||||
|
if char in magazine and ransomNote.count(char) <= magazine.count(char):
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
```
|
||||||
|
|
||||||
### Go:
|
### Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -651,6 +651,23 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
// 递归
|
||||||
|
public int SumOfLeftLeaves(TreeNode root)
|
||||||
|
{
|
||||||
|
if (root == null) return 0;
|
||||||
|
|
||||||
|
int leftValue = SumOfLeftLeaves(root.left);
|
||||||
|
if (root.left != null && root.left.left == null && root.left.right == null)
|
||||||
|
{
|
||||||
|
leftValue += root.left.val;
|
||||||
|
}
|
||||||
|
int rightValue = SumOfLeftLeaves(root.right);
|
||||||
|
return leftValue + rightValue;
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -684,6 +684,38 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
//递归
|
||||||
|
int maxDepth = -1;
|
||||||
|
int res = 0;
|
||||||
|
public int FindBottomLeftValue(TreeNode root)
|
||||||
|
{
|
||||||
|
Traversal(root, 0);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
public void Traversal(TreeNode root, int depth)
|
||||||
|
{
|
||||||
|
if (root.left == null && root.right == null)
|
||||||
|
{
|
||||||
|
if (depth > maxDepth)
|
||||||
|
{
|
||||||
|
maxDepth = depth;
|
||||||
|
res = root.val;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (root.left != null)
|
||||||
|
{
|
||||||
|
Traversal(root.left, depth + 1);
|
||||||
|
}
|
||||||
|
if (root.right != null)
|
||||||
|
{
|
||||||
|
Traversal(root.right, depth + 1);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -46,7 +46,7 @@
|
|||||||
|
|
||||||
从前向后填充就是O(n^2)的算法了,因为每次添加元素都要将添加元素之后的所有元素整体向后移动。
|
从前向后填充就是O(n^2)的算法了,因为每次添加元素都要将添加元素之后的所有元素整体向后移动。
|
||||||
|
|
||||||
**其实很多数组填充类的问题,其做饭都是先预先给数组扩容带填充后的大小,然后在从后向前进行操作。**
|
**其实很多数组填充类的问题,其做法都是先预先给数组扩容带填充后的大小,然后在从后向前进行操作。**
|
||||||
|
|
||||||
这么做有两个好处:
|
这么做有两个好处:
|
||||||
|
|
||||||
@ -162,6 +162,27 @@ class Main {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Go:
|
### Go:
|
||||||
|
````go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main(){
|
||||||
|
var strByte []byte
|
||||||
|
|
||||||
|
fmt.Scanln(&strByte)
|
||||||
|
|
||||||
|
for i := 0; i < len(strByte); i++{
|
||||||
|
if strByte[i] <= '9' && strByte[i] >= '0' {
|
||||||
|
inserElement := []byte{'n','u','m','b','e','r'}
|
||||||
|
strByte = append(strByte[:i], append(inserElement, strByte[i+1:]...)...)
|
||||||
|
i = i + len(inserElement) -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf(string(strByte))
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -214,6 +214,34 @@ public class Main {
|
|||||||
|
|
||||||
|
|
||||||
### Go:
|
### Go:
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func reverse (strByte []byte, l, r int){
|
||||||
|
for l < r {
|
||||||
|
strByte[l], strByte[r] = strByte[r], strByte[l]
|
||||||
|
l++
|
||||||
|
r--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func main(){
|
||||||
|
var str string
|
||||||
|
var target int
|
||||||
|
|
||||||
|
fmt.Scanln(&target)
|
||||||
|
fmt.Scanln(&str)
|
||||||
|
strByte := []byte(str)
|
||||||
|
|
||||||
|
reverse(strByte, 0, len(strByte) - 1)
|
||||||
|
reverse(strByte, 0, target - 1)
|
||||||
|
reverse(strByte, target, len(strByte) - 1)
|
||||||
|
|
||||||
|
fmt.Printf(string(strByte))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### JavaScript:
|
### JavaScript:
|
||||||
|
@ -741,6 +741,99 @@ impl Solution{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
// 前序遍历
|
||||||
|
public IList<int> PreorderTraversal(TreeNode root)
|
||||||
|
{
|
||||||
|
var res = new List<int>();
|
||||||
|
var st = new Stack<TreeNode>();
|
||||||
|
if (root == null) return res;
|
||||||
|
st.Push(root);
|
||||||
|
while (st.Count != 0)
|
||||||
|
{
|
||||||
|
var node = st.Peek();
|
||||||
|
if (node == null)
|
||||||
|
{
|
||||||
|
st.Pop();
|
||||||
|
node = st.Peek();
|
||||||
|
st.Pop();
|
||||||
|
res.Add(node.val);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
st.Pop();
|
||||||
|
if (node.right != null) st.Push(node.right);
|
||||||
|
if (node.left != null) st.Push(node.left);
|
||||||
|
st.Push(node);
|
||||||
|
st.Push(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```C#
|
||||||
|
// 中序遍历
|
||||||
|
public IList<int> InorderTraversal(TreeNode root)
|
||||||
|
{
|
||||||
|
var res = new List<int>();
|
||||||
|
var st = new Stack<TreeNode>();
|
||||||
|
if (root == null) return res;
|
||||||
|
st.Push(root);
|
||||||
|
while (st.Count != 0)
|
||||||
|
{
|
||||||
|
var node = st.Peek();
|
||||||
|
if (node == null)
|
||||||
|
{
|
||||||
|
st.Pop();
|
||||||
|
node = st.Peek();
|
||||||
|
st.Pop();
|
||||||
|
res.Add(node.val);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
st.Pop();
|
||||||
|
if (node.right != null) st.Push(node.right);
|
||||||
|
st.Push(node);
|
||||||
|
st.Push(null);
|
||||||
|
if (node.left != null) st.Push(node.left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```C#
|
||||||
|
// 后序遍历
|
||||||
|
public IList<int> PostorderTraversal(TreeNode root)
|
||||||
|
{
|
||||||
|
var res = new List<int>();
|
||||||
|
var st = new Stack<TreeNode>();
|
||||||
|
if (root == null) return res;
|
||||||
|
st.Push(root);
|
||||||
|
while (st.Count != 0)
|
||||||
|
{
|
||||||
|
var node = st.Peek();
|
||||||
|
if (node == null)
|
||||||
|
{
|
||||||
|
st.Pop();
|
||||||
|
node = st.Peek();
|
||||||
|
st.Pop();
|
||||||
|
res.Add(node.val);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
st.Pop();
|
||||||
|
if (node.left != null) st.Push(node.left);
|
||||||
|
if (node.right != null) st.Push(node.right);
|
||||||
|
st.Push(node);
|
||||||
|
st.Push(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.Reverse(0, res.Count);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -695,6 +695,67 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
// 前序遍历
|
||||||
|
public IList<int> PreorderTraversal(TreeNode root)
|
||||||
|
{
|
||||||
|
var st = new Stack<TreeNode>();
|
||||||
|
var res = new List<int>();
|
||||||
|
if (root == null) return res;
|
||||||
|
st.Push(root);
|
||||||
|
while (st.Count != 0)
|
||||||
|
{
|
||||||
|
var node = st.Pop();
|
||||||
|
res.Add(node.val);
|
||||||
|
if (node.right != null)
|
||||||
|
st.Push(node.right);
|
||||||
|
if (node.left != null)
|
||||||
|
st.Push(node.left);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 中序遍历
|
||||||
|
public IList<int> InorderTraversal(TreeNode root)
|
||||||
|
{
|
||||||
|
var st = new Stack<TreeNode>();
|
||||||
|
var res = new List<int>();
|
||||||
|
var cur = root;
|
||||||
|
while (st.Count != 0 || cur != null)
|
||||||
|
{
|
||||||
|
if (cur != null)
|
||||||
|
{
|
||||||
|
st.Push(cur);
|
||||||
|
cur = cur.left;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cur = st.Pop();
|
||||||
|
res.Add(cur.val);
|
||||||
|
cur = cur.right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
// 后序遍历
|
||||||
|
public IList<int> PostorderTraversal(TreeNode root)
|
||||||
|
{
|
||||||
|
var res = new List<int>();
|
||||||
|
var st = new Stack<TreeNode>();
|
||||||
|
if (root == null) return res;
|
||||||
|
st.Push(root);
|
||||||
|
while (st.Count != 0)
|
||||||
|
{
|
||||||
|
var cur = st.Pop();
|
||||||
|
res.Add(cur.val);
|
||||||
|
if (cur.left != null) st.Push(cur.left);
|
||||||
|
if (cur.right != null) st.Push(cur.right);
|
||||||
|
}
|
||||||
|
res.Reverse(0, res.Count());
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -565,6 +565,60 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### C#
|
||||||
|
```C#
|
||||||
|
// 前序遍历
|
||||||
|
public IList<int> PreorderTraversal(TreeNode root)
|
||||||
|
{
|
||||||
|
var res = new List<int>();
|
||||||
|
if (root == null) return res;
|
||||||
|
Traversal(root, res);
|
||||||
|
return res;
|
||||||
|
|
||||||
|
}
|
||||||
|
public void Traversal(TreeNode cur, IList<int> res)
|
||||||
|
{
|
||||||
|
if (cur == null) return;
|
||||||
|
res.Add(cur.val);
|
||||||
|
Traversal(cur.left, res);
|
||||||
|
Traversal(cur.right, res);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```C#
|
||||||
|
// 中序遍历
|
||||||
|
public IList<int> InorderTraversal(TreeNode root)
|
||||||
|
{
|
||||||
|
var res = new List<int>();
|
||||||
|
if (root == null) return res;
|
||||||
|
Traversal(root, res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
public void Traversal(TreeNode cur, IList<int> res)
|
||||||
|
{
|
||||||
|
if (cur == null) return;
|
||||||
|
Traversal(cur.left, res);
|
||||||
|
res.Add(cur.val);
|
||||||
|
Traversal(cur.right, res);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
```C#
|
||||||
|
// 后序遍历
|
||||||
|
public IList<int> PostorderTraversal(TreeNode root)
|
||||||
|
{
|
||||||
|
var res = new List<int>();
|
||||||
|
if (root == null) return res;
|
||||||
|
Traversal(root, res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
public void Traversal(TreeNode cur, IList<int> res)
|
||||||
|
{
|
||||||
|
if (cur == null) return;
|
||||||
|
Traversal(cur.left, res);
|
||||||
|
Traversal(cur.right, res);
|
||||||
|
res.Add(cur.val);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
Reference in New Issue
Block a user