Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 23, 2017 00:13
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/95aeec46af7507466740866e2c670e51 to your computer and use it in GitHub Desktop.
Save jianminchen/95aeec46af7507466740866e2c670e51 to your computer and use it in GitHub Desktop.
code review - tree to list - IEnumerable - code review link: http://codereview.stackexchange.com/a/74017/123986
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConvertTreeToList_codereview1
{
/*
* http://codereview.stackexchange.com/a/74017/123986
*
* Julia is working on code review in January 22, 2017
*/
class TreeNode
{
public int Index;
public TreeNode Left, Right;
public TreeNode(int value)
{
this.Index = value;
}
}
class Program
{
static void Main(string[] args)
{
// 0
// 2 4
// 6 8 10
// This makes the structure of the tree you're creating more visible
// in the code and it also repeats itself less.
TreeNode root = new TreeNode(0)
{
Left = new TreeNode(2)
{
Left = new TreeNode(6),
Right = new TreeNode(8)
},
Right = new TreeNode(4)
{
Right = new TreeNode(10)
}
};
IEnumerable<int> res = ConvertTreeToList(root);
}
/* Empty list should be represented by an empty list, not by null.
* This would simplify your code, since you wouldn't have to check
* for nulls before invoking the method recursively.
*
*/
private static IEnumerable<int> ConvertTreeToList(TreeNode root)
{
if (root == null)
return Enumerable.Empty<int>();
return new[] { root.Index }
.Concat(ConvertTreeToList(root.Left))
.Concat(ConvertTreeToList(root.Right));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment