Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/e4fca0040d091be836cf0df1b0c1b9ff to your computer and use it in GitHub Desktop.
Save jianminchen/e4fca0040d091be836cf0df1b0c1b9ff to your computer and use it in GitHub Desktop.
Leetcode 94: binary tree inorder traversal - recursive solution, pass online judge.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Leetcode94_BinaryTreeInOrderTraversal
{
/// <summary>
/// Leetcode 94: Binary tree inorder traversal
/// https://leetcode.com/problems/binary-tree-inorder-traversal/description/
/// </summary>
class Program
{
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x)
{
val = x;
}
}
static void Main(string[] args)
{
}
public IList<int> InorderTraversal(TreeNode root) // emtpy tree, with one root node, with more nodes
{
if (root == null)
{
return new List<int>();
}
var left = InorderTraversal(root.left);
left.Add(root.val);
var right = InorderTraversal(root.right);
// add right to left list
if (right.Count > 0)
{
foreach (var item in right)
{
left.Add(item);
}
}
return left;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment