diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md index edb5ea3e..0e99f1c2 100644 --- a/problems/0236.二叉树的最近公共祖先.md +++ b/problems/0236.二叉树的最近公共祖先.md @@ -431,6 +431,19 @@ impl Solution { } } ``` +### C# +```C# +public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) +{ + if (root == null || root == p || root == q) return root; + TreeNode left = LowestCommonAncestor(root.left, p, q); + TreeNode right = LowestCommonAncestor(root.right, p, q); + if (left != null && right != null) return root; + if (left == null && right != null) return right; + if (left != null && right == null) return left; + return null; +} +```

diff --git a/problems/0501.二叉搜索树中的众数.md b/problems/0501.二叉搜索树中的众数.md index 8881200f..5b26d580 100644 --- a/problems/0501.二叉搜索树中的众数.md +++ b/problems/0501.二叉搜索树中的众数.md @@ -1009,6 +1009,46 @@ pub fn find_mode(root: Option>>) -> Vec { res } ``` +### C# +```C# +// 递归 +public class Solution +{ + public List res = new List(); + public int count = 0; + public int maxCount = 0; + public TreeNode pre = null; + public int[] FindMode(TreeNode root) + { + SearchBST(root); + return res.ToArray(); + } + public void SearchBST(TreeNode root) + { + if (root == null) return; + SearchBST(root.left); + if (pre == null) + count = 1; + else if (pre.val == root.val) + count++; + else + count = 1; + + pre = root; + if (count == maxCount) + { + res.Add(root.val); + } + else if (count > maxCount) + { + res.Clear(); + res.Add(root.val); + maxCount = count; + } + SearchBST(root.right); + } +} +```