Skip to content

Commit e3e77a3

Browse files
committed
new soln
1 parent 93853d0 commit e3e77a3

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

1448.GoodNodes.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// 1448. Count Good Nodes in Binary Tree
2+
// Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.
3+
// Return the number of good nodes in the binary tree.
4+
// Example 1:
5+
// Input: root = [3,1,4,3,null,1,5]
6+
// Output: 4
7+
// Explanation: Nodes in blue are good.
8+
// Root Node (3) is always a good node.
9+
// Node 4 -> (3,4) is the maximum value in the path starting from the root.
10+
// Node 5 -> (3,4,5) is the maximum value in the path
11+
// Node 3 -> (3,1,3) is the maximum value in the path.
12+
// Example 2:
13+
// Input: root = [3,3,null,4,2]
14+
// Output: 3
15+
// Explanation: Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.
16+
// Example 3:
17+
// Input: root = [1]
18+
// Output: 1
19+
// Explanation: Root is considered as good.
20+
21+
/**
22+
* Definition for a binary tree node.
23+
* public class TreeNode {
24+
* public int val;
25+
* public TreeNode left;
26+
* public TreeNode right;
27+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
28+
* this.val = val;
29+
* this.left = left;
30+
* this.right = right;
31+
* }
32+
* }
33+
*/
34+
public class Solution {
35+
public int GoodNodes(TreeNode root) {
36+
return 1 + Traverse(root.left,root.val) +Traverse(root.right,root.val);
37+
}
38+
39+
public int Traverse(TreeNode root, int max){
40+
if(root == null)
41+
return 0;
42+
if(max > root.val){
43+
return Traverse(root.left,max) +Traverse(root.right,max);
44+
}
45+
return 1 + Traverse(root.left,root.val) +Traverse(root.right,root.val);
46+
}
47+
}

230.KthSmallest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// 230. Kth Smallest Element in a BST
2+
// Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.
3+
// Example 1:
4+
// Input: root = [3,1,4,null,2], k = 1
5+
// Output: 1
6+
// Example 2:
7+
// Input: root = [5,3,6,2,4,null,null,1], k = 3
8+
// Output: 3
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* public int val;
13+
* public TreeNode left;
14+
* public TreeNode right;
15+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
16+
* this.val = val;
17+
* this.left = left;
18+
* this.right = right;
19+
* }
20+
* }
21+
*/
22+
public class Solution {
23+
Stack<int> st;
24+
public int KthSmallest(TreeNode root, int k) {
25+
st = new Stack<int>();
26+
findsmallest(root,k);
27+
return st.Pop();
28+
}
29+
public void findsmallest(TreeNode root, int k){
30+
if(root == null)
31+
return;
32+
if(st.Count < k)
33+
findsmallest(root.left,k);
34+
if(st.Count < k)
35+
st.Push(root.val);
36+
if(st.Count < k)
37+
findsmallest(root.right,k);
38+
}
39+
}

671.FindSecondMinimumValue.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// 671. Second Minimum Node In a Binary Tree
2+
// Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. More formally, the property root.val = min(root.left.val, root.right.val) always holds.
3+
// Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in the whole tree.
4+
// If no such second minimum value exists, output -1 instead.
5+
// Example 1:
6+
// Input: root = [2,2,5,null,null,5,7]
7+
// Output: 5
8+
// Explanation: The smallest value is 2, the second smallest value is 5.
9+
// Example 2:
10+
// Input: root = [2,2,2]
11+
// Output: -1
12+
// Explanation: The smallest value is 2, but there isn't any second smallest value.
13+
14+
/**
15+
* Definition for a binary tree node.
16+
* public class TreeNode {
17+
* public int val;
18+
* public TreeNode left;
19+
* public TreeNode right;
20+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
21+
* this.val = val;
22+
* this.left = left;
23+
* this.right = right;
24+
* }
25+
* }
26+
*/
27+
public class Solution {
28+
HashSet<int> hset;
29+
public int FindSecondMinimumValue(TreeNode root) {
30+
hset = new HashSet<int>();
31+
FindMin(root);
32+
if(hset.Count < 2)
33+
return -1;
34+
var l = hset.OrderBy(x=>x).ToList();
35+
return l[1];
36+
}
37+
public void FindMin(TreeNode root){
38+
if(root == null)
39+
return;
40+
hset.Add(root.val);
41+
FindMin(root.left);
42+
FindMin(root.right);
43+
}
44+
}

0 commit comments

Comments
 (0)