-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBinaryTreeInorderTraversal.cs
64 lines (57 loc) · 1.86 KB
/
BinaryTreeInorderTraversal.cs
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
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCodeSolutionsLib
{
/// <summary>
/// 94. Binary Tree Inorder Traversal
/// Given a binart tree, return the inorder traversal of its nodes' values.
/// IE) Input : [1, null, 2, 3]
/// Output : [1, 3, 2]
/// Explanation: 1
/// / \
/// * 2
/// / \
/// 3
/// </summary>
public class BinaryTreeInorderTraversal : Solution
{
// Definition for a binary tree node
private class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x)
{
val = x;
}
}
private IList<int> recursiveInorderTraversal(TreeNode root)
{
// InOrder Traversal requires doing a DFS on the left most node,
// Base Case
if (root == null)
{
return new List<int>();
}
IList<int> left = recursiveInorderTraversal(root.left); // Recursively travel left
left.Add(root.val); // Once at the bottom, add the left node value
IList<int> right = recursiveInorderTraversal(root.right); // Recursively travel right
// If a right node exists Add the right node's value to the list.
if (right.Count > 0)
{
foreach (int nodeValue in right)
{
left.Add(nodeValue);
}
}
return left;
}
public override void PrintExample()
{
// TODO: Implement a method to create a BST when passed in an array
throw new NotImplementedException();
}
}
}