Skip to content

Commit 1bc470f

Browse files
committed
Tree test console
1 parent d394c20 commit 1bc470f

File tree

10 files changed

+428
-0
lines changed

10 files changed

+428
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* ==============================================================================
2+
* 功能描述:LevelOrderTraversal
3+
* 创 建 者:gz
4+
* 创建日期:2017/4/19 12:23:31
5+
* ==============================================================================*/
6+
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
10+
//Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
11+
12+
//For example:
13+
//Given binary tree [3,9,20,null,null,15,7],
14+
// 3
15+
// / \
16+
// 9 20
17+
// / \
18+
// 15 7
19+
//return its bottom-up level order traversal as:
20+
//[
21+
// [15,7],
22+
// [9,20],
23+
// [3]
24+
//]
25+
26+
namespace Tree.TreeLib
27+
{
28+
/// <summary>
29+
/// 107. Binary Tree Level Order Traversal II
30+
/// </summary>
31+
public class LevelOrderTraversal
32+
{
33+
34+
public IList<IList<int>> LevelOrderBottom(TreeNode root)
35+
{
36+
var rtn = LevelOrderUp(root);
37+
38+
if (rtn != null && rtn.Count > 0)
39+
{
40+
List<IList<int>> rtn2 = new List<IList<int>>();
41+
rtn2.AddRange(rtn);
42+
rtn2.Reverse(); //Ilist.Reverse() failure!
43+
return rtn2;
44+
}
45+
return new List<IList<int>>();
46+
}
47+
48+
public IList<IList<int>> LevelOrderUp(TreeNode root)
49+
{
50+
if (root == null)
51+
return new List<IList<int>>();
52+
IList<IList<int>> rtn = new List<IList<int>>{new List<int> {root.val}};
53+
var tmp = getNextLevel(new List<TreeNode> { root });
54+
if (tmp != null && tmp.Count > 0)
55+
{
56+
foreach (var item in tmp)
57+
rtn.Add(item);
58+
}
59+
return rtn;
60+
}
61+
62+
private IList<IList<int>> getNextLevel(IList<TreeNode> curLevel)
63+
{
64+
if (curLevel == null || curLevel.Count == 0)
65+
return null;
66+
IList<IList<int>> rtn= new List<IList<int>>();
67+
List<TreeNode> nextn = new List<TreeNode>();
68+
foreach (var item in curLevel)
69+
{
70+
if (item.left != null)
71+
nextn.Add(item.left);
72+
if (item.right != null)
73+
nextn.Add(item.right);
74+
}
75+
if (nextn.Count == 0)
76+
return null;
77+
rtn.Add(nextn.Select(r => r.val).ToList());
78+
var children = getNextLevel(nextn);
79+
if (children != null && children.Count > 0)
80+
{
81+
foreach (var item in children)
82+
rtn.Add(item);
83+
}
84+
return rtn;
85+
}
86+
}
87+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的常规信息通过以下
6+
// 特性集控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("TreeLib")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("TreeLib")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// 将 ComVisible 设置为 false 使此程序集中的类型
18+
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
19+
// 则将该类型上的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("ba192037-6f56-4221-8634-92b12dc8278e")]
24+
25+
// 程序集的版本信息由下面四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 内部版本号
30+
// 修订号
31+
//
32+
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
33+
// 方法是按如下所示使用“*”:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Tree/Tree.TreeLib/Tree.TreeLib.csproj

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{5BEFEB2E-0D66-43FC-934E-0B68EF9E71F9}</ProjectGuid>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Tree.TreeLib</RootNamespace>
12+
<AssemblyName>Tree.TreeLib</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Xml" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="LevelOrderTraversal.cs" />
44+
<Compile Include="Properties\AssemblyInfo.cs" />
45+
<Compile Include="TreeNode.cs" />
46+
<Compile Include="TreeTools.cs" />
47+
</ItemGroup>
48+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
49+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
50+
Other similar extension points exist, see Microsoft.Common.targets.
51+
<Target Name="BeforeBuild">
52+
</Target>
53+
<Target Name="AfterBuild">
54+
</Target>
55+
-->
56+
</Project>

Tree/Tree.TreeLib/TreeNode.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* ==============================================================================
2+
* 功能描述:TreeNode
3+
* 创 建 者:gz
4+
* 创建日期:2017/4/19 12:20:32
5+
* ==============================================================================*/
6+
7+
namespace Tree.TreeLib
8+
{
9+
/// <summary>
10+
/// TreeNode
11+
/// Definition for a binary tree node.
12+
/// </summary>
13+
public class TreeNode
14+
{
15+
public TreeNode(int x)
16+
{
17+
val = x;
18+
}
19+
20+
public int val;
21+
public TreeNode left;
22+
public TreeNode right;
23+
24+
/// <summary>
25+
/// 构建树
26+
/// </summary>
27+
/// <param name="nums">数组,无左右子树,用null表示</param>
28+
/// <returns>树根</returns>
29+
public static TreeNode buildTree(object[] nums)
30+
{
31+
return build(nums, 0);
32+
}
33+
34+
private static TreeNode build(object[] nums, int i)
35+
{
36+
if (i>=nums.Length || nums[i] == null)
37+
return null;
38+
TreeNode root = new TreeNode((int)nums[i]);
39+
root.left = build(nums, 2*i + 1);
40+
root.right = build(nums, 2*i + 2);
41+
return root;
42+
}
43+
}
44+
45+
46+
}

Tree/Tree.TreeLib/TreeTools.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* ==============================================================================
2+
* 功能描述:TreeNode
3+
* 创 建 者:gz
4+
* 创建日期:2017/4/19 12:20:32
5+
* ==============================================================================*/
6+
7+
namespace Tree.TreeLib
8+
{
9+
/// <summary>
10+
/// TreeNode
11+
/// Definition for a binary tree node.
12+
/// </summary>
13+
public class TreeTools
14+
{
15+
16+
/// <summary>
17+
/// 构建树
18+
/// </summary>
19+
/// <param name="nums">数组,无左右子树,用null表示</param>
20+
/// <returns>树根</returns>
21+
public static TreeNode buildTree(object[] nums)
22+
{
23+
return build(nums, 0);
24+
}
25+
26+
private static TreeNode build(object[] nums, int i)
27+
{
28+
if (i>=nums.Length || nums[i] == null)
29+
return null;
30+
TreeNode root = new TreeNode((int)nums[i]);
31+
root.left = build(nums, 2*i + 1);
32+
root.right = build(nums, 2*i + 2);
33+
return root;
34+
}
35+
}
36+
37+
38+
}

Tree/Tree/Program.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Tree.test;
6+
using Tree.TreeLib;
7+
8+
9+
10+
namespace Tree
11+
{
12+
class Program
13+
{
14+
static void Main(string[] args)
15+
{
16+
17+
18+
19+
}
20+
}
21+
}

Tree/Tree/Properties/AssemblyInfo.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的常规信息通过以下
6+
// 特性集控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("Tree")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Tree")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// 将 ComVisible 设置为 false 使此程序集中的类型
18+
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
19+
// 则将该类型上的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("99bdaedd-9617-4457-a3ee-d031fd99bc48")]
24+
25+
// 程序集的版本信息由下面四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 内部版本号
30+
// 修订号
31+
//
32+
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
33+
// 方法是按如下所示使用“*”:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Tree/Tree/Tree.csproj

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{B50199B3-C1D8-488C-826C-3FA2F5BC5B01}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Tree</RootNamespace>
12+
<AssemblyName>Tree</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
<Compile Include="test\testTree.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<ProjectReference Include="..\Tree.TreeLib\Tree.TreeLib.csproj">
52+
<Project>{5BEFEB2E-0D66-43FC-934E-0B68EF9E71F9}</Project>
53+
<Name>Tree.TreeLib</Name>
54+
</ProjectReference>
55+
</ItemGroup>
56+
<ItemGroup />
57+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
58+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
59+
Other similar extension points exist, see Microsoft.Common.targets.
60+
<Target Name="BeforeBuild">
61+
</Target>
62+
<Target Name="AfterBuild">
63+
</Target>
64+
-->
65+
</Project>

0 commit comments

Comments
 (0)