二叉树高频题(下)
| using namespace std; |
| |
| struct TreeNode { |
| int val; |
| TreeNode *left; |
| TreeNode *right; |
| |
| TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| }; |
| |
| class Solution { |
| public: |
| |
| TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) { |
| |
| if (root == nullptr || root == p || root == q) return root; |
| |
| |
| TreeNode *left = lowestCommonAncestor(root->left, p, q); |
| |
| TreeNode *right = lowestCommonAncestor(root->right, p, q); |
| |
| |
| if (left != nullptr && right != nullptr) return root; |
| |
| return left == nullptr ? right : left; |
| } |
| }; |
| using namespace std; |
| |
| struct TreeNode { |
| int val; |
| TreeNode *left; |
| TreeNode *right; |
| |
| TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| }; |
| |
| class Solution { |
| public: |
| TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) { |
| if (root == nullptr || root == p || root == q) return root; |
| |
| if (root->val > p->val && root->val > q->val) |
| return lowestCommonAncestor(root->left, p, q); |
| |
| if (root->val < p->val && root->val < q->val) |
| return lowestCommonAncestor(root->right, p, q); |
| |
| return root; |
| } |
| }; |
| class Solution { |
| public: |
| TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q) { |
| if (root == nullptr || root == p || root == q) return root; |
| TreeNode *left = lowestCommonAncestor(root->left, p, q); |
| TreeNode *right = lowestCommonAncestor(root->right, p, q); |
| if (left == nullptr) return right; |
| if (right == nullptr) return left; |
| return root; |
| } |
| }; |
| #include <vector> |
| |
| using namespace std; |
| |
| 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) {} |
| }; |
| |
| class Solution { |
| public: |
| vector<vector<int>> res; |
| vector<int> path; |
| int target; |
| |
| void dfs(TreeNode *root, int sum) { |
| if (root == nullptr) return; |
| sum += root->val; |
| if (root->left == nullptr && root->right == nullptr && sum == target) { |
| path.emplace_back(root->val); |
| res.emplace_back(vector<int>(path)); |
| |
| path.erase(end(path)); |
| return; |
| } |
| path.emplace_back(root->val); |
| if (root->left != nullptr) dfs(root->left, sum); |
| if (root->right != nullptr) dfs(root->right, sum); |
| |
| path.erase(end(path)); |
| } |
| |
| vector<vector<int>> pathSum(TreeNode *root, int targetSum) { |
| target = targetSum; |
| dfs(root, 0); |
| return res; |
| } |
| }; |
| class Solution { |
| public: |
| vector<vector<int>> res; |
| vector<int> path; |
| |
| unordered_map<TreeNode *, int> map; |
| int target; |
| |
| void dfs(TreeNode *root, int sum) { |
| if (root == nullptr) return; |
| path.emplace_back(root->val); |
| map.emplace(root, path.size() - 1); |
| sum += root->val; |
| if (root->left == nullptr && root->right == nullptr && sum == target) { |
| res.emplace_back(vector<int>(path)); |
| return; |
| } |
| if (root->left != nullptr) { |
| dfs(root->left, sum); |
| |
| path.erase(begin(path) + map[root->left], end(path)); |
| } |
| if (root->right != nullptr) { |
| dfs(root->right, sum); |
| |
| path.erase(begin(path) + map[root->right], end(path)); |
| } |
| } |
| |
| vector<vector<int>> pathSum(TreeNode *root, int targetSum) { |
| target = targetSum; |
| dfs(root, 0); |
| return res; |
| } |
| }; |
| #include <algorithm> |
| |
| using namespace std; |
| |
| 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) {} |
| }; |
| |
| class Solution { |
| public: |
| bool balance; |
| |
| int depth(struct TreeNode *node) { |
| if (!balance || node == nullptr) return 0; |
| int left = depth(node->left); |
| int right = depth(node->right); |
| |
| if (abs(left - right) > 1) balance = false; |
| return max(left, right) + 1; |
| } |
| |
| bool isBalanced(TreeNode *root) { |
| balance = true; |
| depth(root); |
| return balance; |
| } |
| }; |
| using namespace std; |
| |
| 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) {} |
| }; |
| |
| class Solution { |
| public: |
| TreeNode *pre; |
| |
| |
| bool inorder(TreeNode *root) { |
| if (root == nullptr) return true; |
| if (!inorder(root->left)) return false; |
| if (pre != nullptr && pre->val >= root->val) return false; |
| pre = root; |
| return inorder(root->right); |
| } |
| |
| bool isValidBST(TreeNode *root) { |
| pre = nullptr; |
| return inorder(root); |
| } |
| }; |
| class Solution { |
| public: |
| |
| bool dfs(struct TreeNode *root, long long min, long long max) { |
| if (root == nullptr) return true; |
| if (root->val <= min || root->val >= max) return false; |
| return dfs(root->left, min, root->val) && dfs(root->right, root->val, max); |
| } |
| |
| bool isValidBST(TreeNode *root) { |
| return dfs(root, 0x8000000000000000, 0x7fffffffffffffff); |
| } |
| }; |
| using namespace std; |
| |
| 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) {} |
| }; |
| |
| class Solution { |
| public: |
| TreeNode *trimBST(TreeNode *root, int low, int high) { |
| if (root == nullptr) return nullptr; |
| |
| if (root->val < low) return trimBST(root->right, low, high); |
| if (root->val > high) return trimBST(root->left, low, high); |
| root->left = trimBST(root->left, low, high); |
| root->right = trimBST(root->right, low, high); |
| return root; |
| } |
| }; |
| using namespace std; |
| |
| 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) {} |
| }; |
| |
| class Solution { |
| public: |
| |
| int rob(TreeNode *root) { |
| if (root == nullptr) return 0; |
| int money = root->val; |
| if (root->left != nullptr) |
| money += rob(root->left->left) + rob(root->left->right); |
| if (root->right != nullptr) |
| money += rob(root->right->left) + rob(root->right->right); |
| |
| return max(money, rob(root->left) + rob(root->right)); |
| } |
| }; |
| class Solution { |
| public: |
| |
| unordered_map<TreeNode *, int> dp; |
| |
| int robInternal(TreeNode *root) { |
| if (root == nullptr) return 0; |
| |
| if (dp.find(root) != dp.end()) return dp[root]; |
| int money = root->val; |
| if (root->left != nullptr) |
| money += (robInternal(root->left->left) + robInternal(root->left->right)); |
| if (root->right != nullptr) |
| money += (robInternal(root->right->left) + robInternal(root->right->right)); |
| int result = max(money, robInternal(root->left) + robInternal(root->right)); |
| dp[root] = result; |
| return result; |
| } |
| |
| int rob(TreeNode *root) { |
| return robInternal(root); |
| } |
| }; |
| class Solution { |
| public: |
| int rob(TreeNode* root) { |
| |
| vector<int> result = recursive(root); |
| return max(result[0], result[1]); |
| } |
| |
| vector<int> recursive(TreeNode* root) { |
| if (root == nullptr) return {0, 0}; |
| vector<int> res(2); |
| vector<int> left = recursive(root->left); |
| vector<int> right = recursive(root->right); |
| res[0] = max(left[0], left[1]) + max(right[0], right[1]); |
| res[1] = left[0] + right[0] + root->val; |
| return res; |
| } |
| }; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步