mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
@ -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.二叉树的右视图
|
||||||
|
|
||||||
|
@ -1022,6 +1022,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">
|
||||||
|
@ -1018,25 +1018,13 @@ public class Solution {
|
|||||||
```csharp
|
```csharp
|
||||||
//迭代
|
//迭代
|
||||||
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);
|
||||||
{
|
return root;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -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