diff --git "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" index 8881200f5d..5b26d580b2 100644 --- "a/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.md" +++ "b/problems/0501.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\344\274\227\346\225\260.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); + } +} +```