@@ -1640,6 +1640,118 @@ public:
1640
1640
[深度优先搜索](https://leetcode.com/problemset/all/?topicSlugs=depth-first-search)
1641
1641
# 18. 广度优先搜索
1642
1642
[广度优先搜索](https://leetcode.com/problemset/all/?topicSlugs=breadth-first-search)
1643
+ ## 993. Cousins in Binary Tree
1644
+ ```C++
1645
+ class Solution {
1646
+ public:
1647
+ bool isCousins(TreeNode* root, int x, int y) {
1648
+ if (root == nullptr) return false;
1649
+ queue<TreeNode*> tree;
1650
+ tree.push(root);
1651
+ while (!tree.empty()) {
1652
+ int size = tree.size();
1653
+ bool existX = false;
1654
+ bool existY = false;
1655
+ for (int i = 0; i < size; i++) {
1656
+ TreeNode* node = tree.front();
1657
+ tree.pop();
1658
+ if (node->val == x)
1659
+ existX = true;
1660
+ if (node->val == y)
1661
+ existY = true;
1662
+ if (node->left && node->right) {
1663
+ if (node->left->val == x && node->right->val == y)
1664
+ return false;
1665
+ if (node->right->val == x && node->left->val == y)
1666
+ return false;
1667
+ }
1668
+ if (node->left) tree.push(node->left);
1669
+ if (node->right) tree.push(node->right);
1670
+ }
1671
+ if (existX && existY) return true;
1672
+ }
1673
+ return false;
1674
+ }
1675
+ };
1676
+ ```
1677
+ ## 690. Employee Importance
1678
+ 本地测试通过,但是AC不了,不知道原因在哪?
1679
+ ``` C++
1680
+ class Solution {
1681
+ public:
1682
+ int getImportance(vector<Employee* > employees, int id) {
1683
+ int res = 0;
1684
+ int size = employees.size();
1685
+ if (size == 0) return 0;
1686
+ sort(employees.begin(), employees.end());
1687
+ queue<int > sub;
1688
+ sub.push(id);
1689
+ for (auto i : employees) {
1690
+ int nowid = sub.front();
1691
+ if (i->id == nowid) {
1692
+ res += i->importance;
1693
+ if (i->subordinates.size()) {
1694
+ for (auto j : i->subordinates)
1695
+ sub.push(j);
1696
+ }
1697
+ sub.pop();
1698
+ }
1699
+ }
1700
+ return res;
1701
+ }
1702
+ };
1703
+ ```
1704
+ 估计方法不对,加了cmp也不对
1705
+ ```C++
1706
+ bool cmp(Employee* a, Employee* b) {
1707
+ return a->id < b->id;
1708
+ }
1709
+
1710
+ class Solution {
1711
+ public:
1712
+ int getImportance(vector<Employee*> employees, int id) {
1713
+ int res = 0;
1714
+ int size = employees.size();
1715
+ if (size == 0) return 0;
1716
+ sort(employees.begin(), employees.end(),cmp);
1717
+ queue<int> sub;
1718
+ sub.push(id);
1719
+ for (auto i : employees) {
1720
+ int nowid = sub.front();
1721
+ if (i->id == nowid) {
1722
+ res += i->importance;
1723
+ if (i->subordinates.size()) {
1724
+ for (auto j : i->subordinates)
1725
+ sub.push(j);
1726
+ }
1727
+ sub.pop();
1728
+ }
1729
+ }
1730
+ return res;
1731
+ }
1732
+ };
1733
+ ```
1734
+ AC版本
1735
+ ``` C++
1736
+ class Solution {
1737
+ public:
1738
+ int getImportance(vector<Employee* > employees, int id) {
1739
+ unordered_map<int, Employee* > employee;
1740
+
1741
+ for (const auto em : employees) {
1742
+ employee[em->id] = em;
1743
+ }
1744
+ return computeImportance(employee, id);
1745
+ }
1746
+ int computeImportance (unordered_map<int, Employee* >& employee, const int id) {
1747
+ int sum = employee[ id] ->importance;
1748
+ for (const auto em : employee[ id] ->subordinates) {
1749
+ sum += computeImportance(employee, em);
1750
+ }
1751
+ return sum;
1752
+ }
1753
+ };
1754
+ ```
1643
1755
# 19. 并查集UnionFind
1644
1756
[并查集](https://leetcode.com/problemset/all/?topicSlugs=union-find)
1645
1757
# 20. 图
0 commit comments