File tree 2 files changed +81
-0
lines changed
June-LeetCoding-Challenge
23-Count-Complete-Tree-Nodes
2 files changed +81
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 96 ms, faster than 82.24% of C# online submissions for Single Number II.
2
+ // Memory Usage: 25.3 MB, less than 40.54% of C# online submissions for Single Number II.
3
+ public class Solution {
4
+ public int SingleNumber ( int [ ] nums ) {
5
+ var dict = new Dictionary < int , int > ( ) ;
6
+ for ( var i = 0 ; i < nums . Length ; i ++ ) {
7
+ if ( ! dict . ContainsKey ( nums [ i ] ) ) {
8
+ dict [ nums [ i ] ] = 1 ;
9
+ } else {
10
+ dict [ nums [ i ] ] += 1 ;
11
+ }
12
+ }
13
+
14
+ foreach ( KeyValuePair < int , int > kv in dict ) {
15
+ if ( kv . Value == 1 ) {
16
+ return kv . Key ;
17
+ }
18
+ }
19
+
20
+ return - 1 ;
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ public class Solution
2
+ {
3
+ //Solution 1
4
+ // Runtime: 100 ms, faster than 96.07% of C# online submissions for Count Complete Tree Nodes.
5
+ // Memory Usage: 32.3 MB, less than 97.70% of C# online submissions for Count Complete Tree Nodes.
6
+ public int CountNodes ( TreeNode root )
7
+ {
8
+ var count = 0 ;
9
+ if ( root == null )
10
+ {
11
+ return count ;
12
+ }
13
+ DfsPreorderTraversal ( root , ref count ) ;
14
+ return count ;
15
+ }
16
+ public void DfsPreorderTraversal ( TreeNode node , ref int count )
17
+ {
18
+ if ( node != null )
19
+ {
20
+ count ++ ;
21
+ }
22
+ if ( node . left != null )
23
+ {
24
+ DfsPreorderTraversal ( node . left , ref count ) ;
25
+ }
26
+ if ( node . right != null )
27
+ {
28
+ DfsPreorderTraversal ( node . right , ref count ) ;
29
+ }
30
+ return ;
31
+ }
32
+
33
+ //Solution 2
34
+ // Runtime: 112 ms, faster than 50.00% of C# online submissions for Count Complete Tree Nodes.
35
+ // Memory Usage: 32.5 MB, less than 87.10% of C# online submissions for Count Complete Tree Nodes.
36
+ public int CountNodes ( TreeNode root )
37
+ {
38
+ if ( root == null )
39
+ {
40
+ return 0 ;
41
+ }
42
+ return 1 + CountNodes ( root . left ) + CountNodes ( root . right ) ;
43
+ }
44
+ }
45
+
46
+
47
+ /**
48
+ * Definition for a binary tree node.
49
+ * public class TreeNode {
50
+ * public int val;
51
+ * public TreeNode left;
52
+ * public TreeNode right;
53
+ * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
54
+ * this.val = val;
55
+ * this.left = left;
56
+ * this.right = right;
57
+ * }
58
+ * }
59
+ */
You can’t perform that action at this time.
0 commit comments