File tree Expand file tree Collapse file tree 14 files changed +385
-15
lines changed Expand file tree Collapse file tree 14 files changed +385
-15
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 40
40
<Reference Include =" System.Xml" />
41
41
</ItemGroup >
42
42
<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" />
43
54
<Compile Include =" LevelOrderTraversal.cs" />
44
55
<Compile Include =" Properties\AssemblyInfo.cs" />
56
+ <Compile Include =" SameTree.cs" />
45
57
<Compile Include =" TreeNode.cs" />
58
+ <Compile Include =" TreePathSum.cs" />
46
59
<Compile Include =" TreeTools.cs" />
47
60
</ItemGroup >
61
+ <ItemGroup >
62
+ <Content Include =" leetcode-tree.xml" />
63
+ </ItemGroup >
48
64
<Import Project =" $(MSBuildToolsPath)\Microsoft.CSharp.targets" />
49
65
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
50
66
Other similar extension points exist, see Microsoft.Common.targets.
You can’t perform that action at this time.
0 commit comments