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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update.二叉树迭代遍历,C#版
  • Loading branch information
eeee0717 committed Nov 12, 2023
commit 5d9d37f01d59b2fa95001018fb24e4b74e2a04f9
61 changes: 61 additions & 0 deletions problems/二叉树的迭代遍历.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down

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