Created
January 22, 2017 23:54
-
-
Save jianminchen/1e49e8c50063537add1cfbd598d1db65 to your computer and use it in GitHub Desktop.
code review - http://codereview.stackexchange.com/a/73642/123986
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConvertTreeToList_codereview1 | |
{ | |
/* | |
* http://codereview.stackexchange.com/a/73642/123986 | |
*/ | |
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 | |
TreeNode root = new TreeNode(0); | |
root.Left = new TreeNode(2); | |
root.Right = new TreeNode(4); | |
root.Left.Left = new TreeNode(6); | |
root.Left.Right = new TreeNode(8); | |
root.Right.Right = new TreeNode(10); | |
List<int> res = ConvertTreeToList(root); | |
} | |
private static List<int> ConvertTreeToList(TreeNode root) | |
{ | |
var result = new List<int>(); | |
ConvertTreeToList(root, result); | |
return result; | |
} | |
private static void ConvertTreeToList(TreeNode root, List<int> result) | |
{ | |
if (root == null) | |
{ | |
return; | |
} | |
result.Add(root.Index); | |
ConvertTreeToList(root.Left, result); | |
ConvertTreeToList(root.Right, result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment