Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c7fe128

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 9b40338 + 12582fd commit c7fe128

20 files changed

+665
-40
lines changed

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@
397397
* [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md)
398398
* [图论:827.最大人工岛](./problems/0827.最大人工岛.md)
399399
* [图论:127. 单词接龙](./problems/0127.单词接龙.md)
400-
* [图论:841.钥匙和房间](./problems/841.钥匙和房间)
400+
* [图论:841.钥匙和房间](./problems/0841.钥匙和房间.md)
401401
* [图论:463. 岛屿的周长](./problems/0463.岛屿的周长.md)
402-
* [图论:并查集理论基础](./problems/)
402+
* [图论:并查集理论基础](./problems/图论并查集理论基础.md)
403403
* [图论:1971. 寻找图中是否存在路径](./problems/1971.寻找图中是否存在路径.md)
404404
* [图论:684.冗余连接](./problems/0684.冗余连接.md)
405405
* [图论:685.冗余连接II](./problems/0685.冗余连接II.md)

‎problems/0070.爬楼梯完全背包版本.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ int main() {
100100
while (cin >> n >> m) {
101101
vector<int> dp(n + 1, 0);
102102
dp[0] = 1;
103-
for (int i = 1; i <= n; i++) { // 遍历物品
104-
for (int j = 1; j <= m; j++) { // 遍历背包
103+
for (int i = 1; i <= n; i++) { // 遍历背包
104+
for (int j = 1; j <= m; j++) { // 遍历物品
105105
if (i - j >= 0) dp[i] += dp[i - j];
106106
}
107107
}

‎problems/0101.对称二叉树.md‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,53 @@ impl Solution {
897897
}
898898
}
899899
```
900+
### C#
901+
```C#
902+
// 递归
903+
public bool IsSymmetric(TreeNode root)
904+
{
905+
if (root == null) return true;
906+
return Compare(root.left, root.right);
907+
}
908+
public bool Compare(TreeNode left, TreeNode right)
909+
{
910+
if(left == null && right != null) return false;
911+
else if(left != null && right == null ) return false;
912+
else if(left == null && right == null) return true;
913+
else if(left.val != right.val) return false;
914+
915+
var outside = Compare(left.left, right.right);
916+
var inside = Compare(left.right, right.left);
917+
918+
return outside&&inside;
919+
}
920+
```
921+
``` C#
922+
// 迭代法
923+
public bool IsSymmetric(TreeNode root)
924+
{
925+
if (root == null) return true;
926+
var st = new Stack<TreeNode>();
927+
st.Push(root.left);
928+
st.Push(root.right);
929+
while (st.Count != 0)
930+
{
931+
var left = st.Pop();
932+
var right = st.Pop();
933+
if (left == null && right == null)
934+
continue;
935+
936+
if ((left == null || right == null || (left.val != right.val)))
937+
return false;
938+
939+
st.Push(left.left);
940+
st.Push(right.right);
941+
st.Push(left.right);
942+
st.Push(right.left);
943+
}
944+
return true;
945+
}
946+
```
900947

901948
<p align="center">
902949
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

‎problems/0102.二叉树的层序遍历.md‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,32 @@ impl Solution {
462462
}
463463
}
464464
```
465+
### C#
466+
```C#
467+
public IList<IList<int>> LevelOrder(TreeNode root)
468+
{
469+
var res = new List<IList<int>>();
470+
var que = new Queue<TreeNode>();
471+
if (root == null) return res;
472+
que.Enqueue(root);
473+
while (que.Count != 0)
474+
{
475+
var size = que.Count;
476+
var vec = new List<int>();
477+
for (int i = 0; i < size; i++)
478+
{
479+
var cur = que.Dequeue();
480+
vec.Add(cur.val);
481+
if (cur.left != null) que.Enqueue(cur.left);
482+
if (cur.right != null) que.Enqueue(cur.right);
483+
}
484+
res.Add(vec);
485+
486+
487+
}
488+
return res;
489+
}
490+
```
465491

466492

467493
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
@@ -798,6 +824,31 @@ impl Solution {
798824
}
799825
}
800826
```
827+
### C#
828+
```C#
829+
public IList<IList<int>> LevelOrderBottom(TreeNode root)
830+
{
831+
var res = new List<IList<int>>();
832+
var que = new Queue<TreeNode>();
833+
if (root == null) return res;
834+
que.Enqueue(root);
835+
while (que.Count != 0)
836+
{
837+
var size = que.Count;
838+
var vec = new List<int>();
839+
for (int i = 0; i < size; i++)
840+
{
841+
var cur = que.Dequeue();
842+
vec.Add(cur.val);
843+
if (cur.left != null) que.Enqueue(cur.left);
844+
if (cur.right != null) que.Enqueue(cur.right);
845+
}
846+
res.Add(vec);
847+
}
848+
res.Reverse();
849+
return res;
850+
}
851+
```
801852

802853
## 199.二叉树的右视图
803854

‎problems/0104.二叉树的最大深度.md‎

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -738,26 +738,36 @@ function maxDepth(root: TreeNode | null): number {
738738

739739
```typescript
740740
// 后续遍历(自下而上)
741-
function maxDepth(root: TreeNode | null): number {
742-
if (root === null) return 0;
743-
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
744-
};
741+
function maxDepth(root: Node | null): number {
742+
if (root === null) return 0
743+
let depth = 0
744+
for (let i = 0; i < root.children.length; i++) {
745+
depth = Math.max(depth, maxDepth(root.children[i]))
746+
}
747+
return depth + 1
748+
}
745749

746750
// 前序遍历(自上而下)
747-
function maxDepth(root: TreeNode | null): number {
748-
function recur(node: TreeNode | null, count: number) {
749-
if (node === null) {
750-
resMax = resMax > count ? resMax : count;
751-
return;
751+
function maxDepth(root: Node | null): number {
752+
if (root === null) return 0
753+
754+
let depth: number = 0
755+
const queue: Array<Node | null> = []
756+
queue.push(root)
757+
758+
while (queue.length > 0) {
759+
let len = queue.length
760+
depth++
761+
for (let i = 0; i < len; i++) {
762+
// 当前层遍历
763+
let curNode: Node | null = queue.shift()!
764+
for (let j = 0; j < curNode.children.length; j++) {
765+
if (curNode.children[j]) queue.push(curNode.children[j])
766+
}
752767
}
753-
recur(node.left, count + 1);
754-
recur(node.right, count + 1);
755768
}
756-
let resMax: number = 0;
757-
let count: number = 0;
758-
recur(root, count);
759-
return resMax;
760-
};
769+
return depth
770+
}
761771

762772

763773
```
@@ -1022,6 +1032,61 @@ impl Solution {
10221032
max_depth
10231033
}
10241034
```
1035+
### C#
1036+
```C#
1037+
// 递归法
1038+
public int MaxDepth(TreeNode root) {
1039+
if(root == null) return 0;
1040+
1041+
int leftDepth = MaxDepth(root.left);
1042+
int rightDepth = MaxDepth(root.right);
1043+
1044+
return 1 + Math.Max(leftDepth, rightDepth);
1045+
}
1046+
```
1047+
```C#
1048+
// 前序遍历
1049+
int result = 0;
1050+
public int MaxDepth(TreeNode root)
1051+
{
1052+
if (root == null) return result;
1053+
GetDepth(root, 1);
1054+
return result;
1055+
}
1056+
public void GetDepth(TreeNode root, int depth)
1057+
{
1058+
result = depth > result ? depth : result;
1059+
if (root.left == null && root.right == null) return;
1060+
1061+
if (root.left != null)
1062+
GetDepth(root.left, depth + 1);
1063+
if (root.right != null)
1064+
GetDepth(root.right, depth + 1);
1065+
return;
1066+
}
1067+
```
1068+
```C#
1069+
// 迭代法
1070+
public int MaxDepth(TreeNode root)
1071+
{
1072+
int depth = 0;
1073+
Queue<TreeNode> que = new();
1074+
if (root == null) return depth;
1075+
que.Enqueue(root);
1076+
while (que.Count != 0)
1077+
{
1078+
int size = que.Count;
1079+
depth++;
1080+
for (int i = 0; i < size; i++)
1081+
{
1082+
var node = que.Dequeue();
1083+
if (node.left != null) que.Enqueue(node.left);
1084+
if (node.right != null) que.Enqueue(node.right);
1085+
}
1086+
}
1087+
return depth;
1088+
}
1089+
```
10251090

10261091
<p align="center">
10271092
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

‎problems/0110.平衡二叉树.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,31 @@ impl Solution {
908908
}
909909
}
910910
```
911+
### C#
912+
```C#
913+
public bool IsBalanced(TreeNode root)
914+
{
915+
return GetHeight(root) == -1 ? false : true;
916+
}
917+
public int GetHeight(TreeNode root)
918+
{
919+
if (root == null) return 0;
920+
int left = GetHeight(root.left);
921+
if (left == -1) return -1;
922+
int right = GetHeight(root.right);
923+
if (right == -1) return -1;
924+
int res;
925+
if (Math.Abs(left - right) > 1)
926+
{
927+
res = -1;
928+
}
929+
else
930+
{
931+
res = 1 + Math.Max(left, right);
932+
}
933+
return res;
934+
}
935+
```
911936

912937
<p align="center">
913938
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

‎problems/0111.二叉树的最小深度.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,49 @@ impl Solution {
708708
}
709709
}
710710
```
711+
### C#
712+
```C#
713+
// 递归
714+
public int MinDepth(TreeNode root)
715+
{
716+
if (root == null) return 0;
717+
int left = MinDepth(root.left);
718+
int right = MinDepth(root.right);
719+
if (root.left == null && root.right != null)
720+
return 1+right;
721+
else if(root.left!=null && root.right == null)
722+
return 1+left;
723+
724+
int res = 1 + Math.Min(left, right);
725+
return res;
726+
}
727+
```
728+
```C#
729+
// 迭代
730+
public int MinDepth(TreeNode root)
731+
{
732+
if (root == null) return 0;
733+
int depth = 0;
734+
var que = new Queue<TreeNode>();
735+
que.Enqueue(root);
736+
while (que.Count > 0)
737+
{
738+
int size = que.Count;
739+
depth++;
740+
for (int i = 0; i < size; i++)
741+
{
742+
var node = que.Dequeue();
743+
if (node.left != null)
744+
que.Enqueue(node.left);
745+
if (node.right != null)
746+
que.Enqueue(node.right);
747+
if (node.left == null && node.right == null)
748+
return depth;
749+
}
750+
}
751+
return depth;
752+
}
753+
```
711754

712755
<p align="center">
713756
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

‎problems/0112.路径总和.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,17 @@ impl Solution {
15111511
}
15121512
}
15131513

1514+
```
1515+
### C#
1516+
```C#
1517+
// 0112.路径总和
1518+
// 递归
1519+
public bool HasPathSum(TreeNode root, int targetSum)
1520+
{
1521+
if (root == null) return false;
1522+
if (root.left == null && root.right == null && targetSum == root.val) return true;
1523+
return HasPathSum(root.left, targetSum - root.val) || HasPathSum(root.right, targetSum - root.val);
1524+
}
15141525
```
15151526

15161527
<p align="center">

‎problems/0222.完全二叉树的节点个数.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,31 @@ impl Solution {
867867
}
868868
}
869869
```
870+
### C#
871+
```C#
872+
// 递归
873+
public int CountNodes(TreeNode root)
874+
{
875+
if (root == null) return 0;
876+
var left = root.left;
877+
var right = root.right;
878+
int leftDepth = 0, rightDepth = 0;
879+
while (left != null)
880+
{
881+
left = left.left;
882+
leftDepth++;
883+
}
884+
while (right != null)
885+
{
886+
right = right.right;
887+
rightDepth++;
888+
}
889+
if (leftDepth == rightDepth)
890+
return (int)Math.Pow(2, leftDepth+1) - 1;
891+
return CountNodes(root.left) + CountNodes(root.right) + 1;
892+
893+
}
894+
```
870895

871896
<p align="center">
872897
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /