summaryrefslogtreecommitdiff
path: root/cpp/865.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-02-23 21:07:19 +0800
committercrupest <crupest@outlook.com>2021-02-23 21:07:19 +0800
commitd8f3b40085619cb680c8f227c65a1f5acc393223 (patch)
tree6a38e3a6c79276fc396259ef962d17236dbed569 /cpp/865.cpp
parentb0162802ad9723c678e495f29ca2f0fc0af2eff1 (diff)
downloadsolutions-d8f3b40085619cb680c8f227c65a1f5acc393223.tar.gz
solutions-d8f3b40085619cb680c8f227c65a1f5acc393223.tar.bz2
solutions-d8f3b40085619cb680c8f227c65a1f5acc393223.zip
Move leetcode solutions to subdir.
Diffstat (limited to 'cpp/865.cpp')
-rw-r--r--cpp/865.cpp60
1 files changed, 0 insertions, 60 deletions
diff --git a/cpp/865.cpp b/cpp/865.cpp
deleted file mode 100644
index 8981a02..0000000
--- a/cpp/865.cpp
+++ /dev/null
@@ -1,60 +0,0 @@
-struct TreeNode
-{
- int val;
- TreeNode *left;
- TreeNode *right;
- TreeNode() : val(0), left(nullptr), right(nullptr) {}
- TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
-};
-
-#include <unordered_set>
-
-class Solution
-{
-public:
- int max_depth = -1;
- std::unordered_set<TreeNode *> deepest_nodes;
-
- void record_depth(TreeNode *root, int depth)
- {
- if (depth > max_depth)
- {
- max_depth = depth;
- deepest_nodes.clear();
- }
-
- if (depth == max_depth)
- deepest_nodes.insert(root);
-
- if (root->left != nullptr)
- record_depth(root->left, depth + 1);
- if (root->right != nullptr)
- record_depth(root->right, depth + 1);
- }
-
- TreeNode *find_common_ancestor(TreeNode *root)
- {
- if (root == nullptr)
- return nullptr;
- if (deepest_nodes.find(root) != deepest_nodes.cend())
- return root;
-
- auto left = find_common_ancestor(root->left);
- auto right = find_common_ancestor(root->right);
-
- if (left != nullptr && right != nullptr)
- return root;
- if (left != nullptr)
- return left;
- if (right != nullptr)
- return right;
- return nullptr;
- }
-
- TreeNode *subtreeWithAllDeepest(TreeNode *root)
- {
- record_depth(root, 0);
- return find_common_ancestor(root);
- }
-};