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

[pull] master from youngyangyang04:master #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 11 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
09835e7
📝 Update 0383.赎金信.md with python3
qiu121 Nov 10, 2023
8945fd9
Update.二叉树的递归遍历,添加C#版
eeee0717 Nov 11, 2023
5d9d37f
Update.二叉树迭代遍历,C#版
eeee0717 Nov 12, 2023
ce2ffd0
📝 Update 0344.反转字符串.md with python3
qiu121 Nov 12, 2023
436bd5a
Update.统一迭代法,添加C#版
eeee0717 Nov 13, 2023
aad5029
Update 102.二叉树的层序遍历,添加C#版
eeee0717 Nov 14, 2023
dba4e9d
Update226.翻转二叉树,简化C#代码
eeee0717 Nov 15, 2023
b51573e
Update101.对称二叉树,添加C#版
eeee0717 Nov 15, 2023
3ffca33
Update104.二叉树的最大深度,添加C#版
eeee0717 Nov 16, 2023
2a8c571
Merge pull request #2333 from qiu121/master
youngyangyang04 Nov 16, 2023
3334e5f
Merge pull request #2334 from eeee0717/master
youngyangyang04 Nov 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions problems/0101.对称二叉树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down
51 changes: 51 additions & 0 deletions problems/0102.二叉树的层序遍历.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```


**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
Expand Down Expand Up @@ -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.二叉树的右视图

Expand Down
55 changes: 55 additions & 0 deletions problems/0104.二叉树的最大深度.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,61 @@ impl Solution {
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">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down
26 changes: 7 additions & 19 deletions problems/0226.翻转二叉树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1018,25 +1018,13 @@ public class Solution {
```csharp
//迭代
public class Solution {
public TreeNode InvertTree(TreeNode root) {
if (root == null) return null;
Stack<TreeNode> stack=new Stack<TreeNode>();
stack.Push(root);
while(stack.Count>0)
{
TreeNode node = stack.Pop();
swap(node);
if(node.right!=null) stack.Push(node.right);
if(node.left!=null) stack.Push(node.left);
}
return root;
}

public void swap(TreeNode node) {
TreeNode temp = node.left;
node.left = node.right;
node.right = temp;
}
public TreeNode InvertTree(TreeNode root) {
if(root == null) return root;
(root.left,root.right) = (root.right, root.left);
InvertTree(root.left);
InvertTree(root.right);
return root;
}
}
```

Expand Down
14 changes: 14 additions & 0 deletions problems/0344.反转字符串.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,20 @@ class Solution:
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
Expand Down
13 changes: 13 additions & 0 deletions problems/0383.赎金信.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,19 @@ class Solution:
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
Expand Down
93 changes: 93 additions & 0 deletions problems/二叉树的统一迭代法.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down
Loading

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