-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0145-Binary-tree-postorder-traversal.cs
More file actions
70 lines (58 loc) · 1.78 KB
/
Copy path0145-Binary-tree-postorder-traversal.cs
File metadata and controls
70 lines (58 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Solution._0145.Binary_tree_postorder_traversal
{
public class _0145_Binary_tree_postorder_traversal
{
/// <summary>
/// Iteratively solution
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
public IList<int> PostorderTraversal(TreeNode root)
{
IList<int> result = new List<int>();
if (root == null) return result;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.Push(root);
while (stack.Count > 0)
{
TreeNode current = stack.Pop();
if (current != null)
{
result.Add(current.val);
if (current.left != null)
stack.Push(current.left);
if (current.right != null)
stack.Push(current.right);
}
}
result.Reverse();
return result;
}
/// <summary>
/// Recursive solution
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
//public IList<int> PostorderTraversal(TreeNode root)
//{
// IList<int> res = new List<int>();
// if (root == null) return res;
// PostOrder(root, ref res);
// return res;
//}
//private void PostOrder(TreeNode node, ref IList<int> res)
//{
// if (node != null)
// {
// PostOrder(node.left, ref res);
// PostOrder(node.right, ref res);
// res.Add(node.val);
// }
//}
}
}