Skip to content

Commit 5e1c83b

Browse files
committed
tree tag more than 10 questions solution
1 parent 7958195 commit 5e1c83b

14 files changed

+385
-15
lines changed

Tree/Tree.TreeLib/BanlancedTree.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+
6+
namespace Tree.TreeLib
7+
{
8+
public class BanlancedTree
9+
{
10+
public bool IsBalanced(TreeNode root)
11+
{
12+
if (root == null)
13+
return true;
14+
if (Math.Abs(root.left.Height - root.right.Height) > 1)
15+
return false;
16+
return IsBalanced(root.left) && IsBalanced(root.right);
17+
}
18+
19+
20+
}
21+
}

Tree/Tree.TreeLib/CommonAncester.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tree.TreeLib
7+
{
8+
public class CommonAncester
9+
{
10+
public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
11+
{
12+
if (root == null)
13+
return null;
14+
if (p == root || q == root) //等于root,则最小祖先一定为root
15+
return root;
16+
bool pOnLeft = isOn(root.left, p);
17+
bool qOnLeft = isOn(root.left, q);
18+
if (pOnLeft != qOnLeft)
19+
return root;
20+
if (pOnLeft == true && qOnLeft == true)
21+
return LowestCommonAncestor(root.left, p, q);
22+
return LowestCommonAncestor(root.right, p, q);
23+
}
24+
//位于node上吗
25+
private bool isOn(TreeNode node, TreeNode goal)
26+
{
27+
if (goal == null)
28+
return true;
29+
if (node == null)
30+
return false;
31+
if (node == goal)
32+
return true;
33+
if (isOn(node.left, goal) == true || isOn(node.right, goal) == true)
34+
return true;
35+
return false;
36+
}
37+
}
38+
}

Tree/Tree.TreeLib/DiameterTree.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tree.TreeLib
7+
{
8+
public class DiameterTree
9+
{
10+
public int DiameterOfBinaryTree(TreeNode root)
11+
{
12+
if (root == null)
13+
return 0;
14+
int max = Math.Max(nodeDiameter(root), DiameterOfBinaryTree(root.left));
15+
return Math.Max(max, DiameterOfBinaryTree(root.right));
16+
}
17+
18+
private int nodeDiameter(TreeNode node)
19+
{
20+
if (node == null)
21+
return 0;
22+
int sum=0;
23+
if (node.left != null)
24+
sum += node.left.Height;
25+
if (node.right != null)
26+
sum += node.right.Height;
27+
return sum;
28+
}
29+
30+
31+
}
32+
}
33+
34+
35+
36+

Tree/Tree.TreeLib/InvertTree.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tree.TreeLib
7+
{
8+
// Invert a binary tree.
9+
10+
// 4
11+
// / \
12+
// 2 7
13+
// / \ / \
14+
//1 3 6 9
15+
//to
16+
// 4
17+
// / \
18+
// 7 2
19+
// / \ / \
20+
//9 6 3 1
21+
public class InvertTree
22+
{
23+
public TreeNode Invert(TreeNode root)
24+
{
25+
if (root == null)
26+
return null;
27+
var tmp = root.left;
28+
root.left = root.right;
29+
root.right = tmp;
30+
Invert(root.left);
31+
Invert(root.right);
32+
return root;
33+
}
34+
}
35+
}

Tree/Tree.TreeLib/LeftLeafSum.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tree.TreeLib
7+
{
8+
public class LeftLeafSum
9+
{
10+
public int SumOfLeftLeaves(TreeNode root)
11+
{
12+
if (root == null)
13+
return 0;
14+
int sum=0;
15+
if (root.left!=null && root.isLeaf())//左叶子
16+
sum += root.left.val;
17+
sum += SumOfLeftLeaves(root.left);
18+
sum += SumOfLeftLeaves(root.right);
19+
return sum;
20+
}
21+
}
22+
}

Tree/Tree.TreeLib/MaxMinDepth.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tree.TreeLib
7+
{
8+
public class MaxMinDepth
9+
{
10+
11+
public int MaxDepth(TreeNode root)
12+
{
13+
if (root == null)
14+
return 0;
15+
return Math.Max(MaxDepth(root.left), MaxDepth(root.right)) + 1;
16+
}
17+
18+
public int MinDepth(TreeNode root)
19+
{
20+
if (root == null)
21+
return 0;
22+
return Math.Min(MinDepth(root.left), MinDepth(root.right)) + 1;
23+
}
24+
25+
}
26+
}

Tree/Tree.TreeLib/OnePathSum.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tree.TreeLib
7+
{
8+
//Given a binary tree and a sum, determine if the tree has a root-to-leaf path
9+
//such that adding up all the values along the path equals the given sum.
10+
11+
public class OnePathSum
12+
{
13+
public bool HasPathSum(TreeNode root, int sum)
14+
{
15+
if (root == null)
16+
return false;
17+
if (root.val == sum && root.isLeaf()) //判断是否加到了叶子
18+
return true;
19+
//求以root为根的路径val和等于sum的
20+
if (HasPathSum(root.left, sum - root.val) == true)
21+
return true;
22+
return HasPathSum(root.right, sum - root.val);
23+
}
24+
}
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tree.TreeLib
7+
{
8+
public class SortArrayToBalanced
9+
{
10+
//Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
11+
//nums = [1 2 3 4 5]
12+
public TreeNode SortedArrayToBST(int[] nums)
13+
{
14+
if (nums.Length == 0)
15+
return null;
16+
return subTree(nums, 0, nums.Length-1);
17+
18+
}
19+
20+
private TreeNode subTree(int[] nums, int begin, int end)
21+
{
22+
if (begin > end)
23+
return null;
24+
int mid = begin + (end - begin) / 2;
25+
TreeNode root = new TreeNode(nums[mid]);
26+
root.left = subTree(nums, begin, mid-1);
27+
root.right = subTree(nums, mid + 1, end);
28+
return root;
29+
}
30+
31+
}
32+
}

Tree/Tree.TreeLib/SymmetricTree .cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Tree.TreeLib
7+
{
8+
public class SymmetricTree
9+
{
10+
public bool IsSymmetric(TreeNode root)
11+
{
12+
if (root == null)
13+
return true;
14+
if (root.left == null && root.right == null)
15+
return true;
16+
if (root.left == null || root.right == null)
17+
return false;
18+
if (root.left.val != root.right.val)
19+
return false;
20+
TreeNode invertRight=invertTree(root.right);
21+
return isSameTree(root.left, invertRight);
22+
}
23+
24+
//反转
25+
private TreeNode invertTree(TreeNode root)
26+
{
27+
if (root == null)
28+
return null;
29+
var tmp = root.left;
30+
root.left = root.right;
31+
root.right = tmp;
32+
invertTree(root.left);
33+
invertTree(root.right);
34+
return root;
35+
}
36+
37+
//比较树是否相等
38+
private bool isSameTree(TreeNode p, TreeNode q)
39+
{
40+
if (p == null && q == null) return true;
41+
if (p == null || q == null) return false;
42+
return p.val == q.val &&
43+
isSameTree(p.left, q.left) &&
44+
isSameTree(p.right, q.right);
45+
}
46+
47+
}
48+
}

Tree/Tree.TreeLib/Tree.TreeLib.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,27 @@
4040
<Reference Include="System.Xml" />
4141
</ItemGroup>
4242
<ItemGroup>
43+
<Compile Include="BanlancedTree.cs" />
44+
<Compile Include="CommonAncester.cs" />
45+
<Compile Include="DiameterTree.cs" />
46+
<Compile Include="FindModes.cs" />
47+
<Compile Include="InvertTree.cs" />
48+
<Compile Include="LeftLeafSum.cs" />
49+
<Compile Include="MaxMinDepth.cs" />
50+
<Compile Include="OnePathSum.cs" />
51+
<Compile Include="SortArrayToBalanced.cs" />
52+
<Compile Include="SymmetricTree .cs" />
53+
<Compile Include="TreePaths.cs" />
4354
<Compile Include="LevelOrderTraversal.cs" />
4455
<Compile Include="Properties\AssemblyInfo.cs" />
56+
<Compile Include="SameTree.cs" />
4557
<Compile Include="TreeNode.cs" />
58+
<Compile Include="TreePathSum.cs" />
4659
<Compile Include="TreeTools.cs" />
4760
</ItemGroup>
61+
<ItemGroup>
62+
<Content Include="leetcode-tree.xml" />
63+
</ItemGroup>
4864
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
4965
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5066
Other similar extension points exist, see Microsoft.Common.targets.

0 commit comments

Comments
 (0)