二叉树高频题(上)
| #include <vector> |
| #include <iostream> |
| #include <algorithm> |
| #include <queue> |
| |
| 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>> levelOrder(TreeNode *root) { |
| if (root == nullptr) return vector<vector<int>>(); |
| vector<vector<int>> res; |
| queue<TreeNode *> q; |
| q.emplace(root); |
| |
| while (!q.empty()) { |
| int len = q.size(); |
| vector<int> curLever; |
| |
| for (int i = 0; i < len; ++i) { |
| TreeNode *node = q.front(); |
| q.pop(); |
| curLever.emplace_back(node->val); |
| |
| if (node->left != nullptr) q.emplace(node->left); |
| if (node->right != nullptr) q.emplace(node->right); |
| } |
| res.emplace_back(curLever); |
| } |
| return res; |
| } |
| }; |
| #include <vector> |
| #include <iostream> |
| #include <algorithm> |
| #include <queue> |
| #include <stack> |
| |
| 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>> zigzagLevelOrder(TreeNode *root) { |
| if (root == nullptr) return vector<vector<int>>(); |
| vector<vector<int>> res; |
| stack<int> stk; |
| queue<TreeNode *> q; |
| q.emplace(root); |
| bool flag = true; |
| |
| while (!q.empty()) { |
| int len = q.size(); |
| vector<int> curLever; |
| |
| for (int i = 0; i < len; ++i) { |
| TreeNode *node = q.front(); |
| q.pop(); |
| if (flag == true) { |
| curLever.emplace_back(node->val); |
| } else { |
| stk.emplace(node->val); |
| } |
| |
| if (node->left != nullptr) q.emplace(node->left); |
| if (node->right != nullptr) q.emplace(node->right); |
| } |
| flag = !flag; |
| while (!stk.empty()) { |
| curLever.emplace_back(stk.top()); |
| stk.pop(); |
| } |
| res.emplace_back(curLever); |
| } |
| return res; |
| } |
| }; |
| #include <vector> |
| #include <iostream> |
| #include <algorithm> |
| #include <queue> |
| #include <stack> |
| |
| 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 widthOfBinaryTree(TreeNode *root) { |
| if (root == nullptr) return 1; |
| int res = 1; |
| |
| queue<pair<TreeNode *, unsigned long long>> q; |
| q.emplace(make_pair(root, 0)); |
| |
| while (!q.empty()) { |
| int len = q.size(); |
| |
| unsigned long long firstIndex; |
| unsigned long long lastIndex; |
| |
| for (int i = 0; i < len; ++i) { |
| auto p = q.front(); |
| q.pop(); |
| TreeNode *node = p.first; |
| unsigned long long index = p.second; |
| if (i == 0) firstIndex = index; |
| if (i == len - 1) lastIndex = index; |
| |
| if (node->left != nullptr) q.emplace(make_pair(node->left, 2 * index + 1)); |
| if (node->right != nullptr) q.emplace(make_pair(node->right, 2 * index + 2)); |
| } |
| |
| res = lastIndex - firstIndex + 1 > res ? lastIndex - firstIndex + 1 : res; |
| } |
| return res; |
| } |
| }; |
| #include <vector> |
| #include <iostream> |
| #include <algorithm> |
| #include <queue> |
| #include <stack> |
| #include <unordered_map> |
| |
| 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: |
| unsigned long long result = 0; |
| |
| unordered_map<int, unsigned long long> map; |
| |
| int widthOfBinaryTree(TreeNode *root) { |
| dfs(root, 0, 0); |
| return result; |
| } |
| |
| void dfs(TreeNode *node, unsigned long long nodeIndex, int level) { |
| if (node == nullptr) return; |
| if (map.count(level) == 0) map[level] = nodeIndex; |
| result = max(result, nodeIndex - map[level] + 1); |
| dfs(node->left, 2 * nodeIndex + 1, level + 1); |
| dfs(node->right, 2 * nodeIndex + 2, level + 1); |
| } |
| }; |
| #include <vector> |
| #include <algorithm> |
| |
| 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 maxDepth(TreeNode *root) { |
| if (root == nullptr) return 0; |
| int depth = 0; |
| const int size = 5002; |
| |
| struct TreeNode *queue[size]; |
| int front = 0, rear = 0; |
| queue[rear++] = root; |
| |
| while (front != rear) { |
| int count = (rear - front + size) % size; |
| |
| depth++; |
| while (count-- > 0) { |
| struct TreeNode *node = queue[(front++) % size]; |
| if (node->left != nullptr) queue[(rear++) % size] = node->left; |
| if (node->right != nullptr) queue[(rear++) % size] = node->right; |
| } |
| } |
| return depth; |
| } |
| }; |
| #include <vector> |
| #include <algorithm> |
| |
| 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 maxDepth(TreeNode *root) { |
| if (root == nullptr) return 0; |
| int left = maxDepth(root->left); |
| int right = maxDepth(root->right); |
| return (left > right ? left : right) + 1; |
| } |
| }; |
| #include <vector> |
| #include <algorithm> |
| |
| 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 minDepth(TreeNode *root) { |
| if (root == nullptr) return 0; |
| const int size = 50002; |
| struct TreeNode *queue[size]; |
| int front = 0; |
| int rear = 0; |
| int depth = 0; |
| queue[rear++] = root; |
| |
| while (front != rear) { |
| int count = (rear - front + size) % size; |
| depth++; |
| while (count-- > 0) { |
| struct TreeNode *node = queue[(front++) % size]; |
| |
| if (node->left == nullptr && node->right == nullptr) |
| return depth; |
| if (node->left != nullptr)queue[(rear++) % size] = node->left; |
| if (node->right != nullptr) queue[(rear++) % size] = node->right; |
| } |
| } |
| |
| return depth; |
| } |
| }; |
| #include <vector> |
| #include <algorithm> |
| |
| 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 minDp; |
| |
| void dfs(TreeNode *node, int currentDepth) { |
| if (node == nullptr) return; |
| currentDepth++; |
| |
| if ((node->left == nullptr && node->right == nullptr) |
| && currentDepth < minDp) |
| minDp = currentDepth; |
| dfs(node->left, currentDepth); |
| dfs(node->right, currentDepth); |
| } |
| |
| int minDepth(TreeNode *root) { |
| if (root == nullptr) return 0; |
| minDp = INT_MAX; |
| dfs(root, 0); |
| return minDp; |
| } |
| }; |
| #include <vector> |
| #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: |
| |
| int minDepth(TreeNode *root) { |
| if (root == nullptr) return 0; |
| if (root->left == nullptr && root->right == nullptr) return 1; |
| int lD = INT_MAX; |
| int rD = INT_MAX; |
| if (root->left != nullptr) lD = minDepth(root->left); |
| if (root->right != nullptr) rD = minDepth(root->right); |
| return min(lD, rD) + 1; |
| } |
| }; |
- 二叉树可以通过先序、后序或者按层遍历的方式序列化和反序列化
- 但是,二叉树无法通过中序遍历的方式实现序列化和反序列化
- 因为不同的两棵树,可能得到同样的中序序列,即便补了空位置也可能一样。
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <regex> |
| #include <iostream> |
| |
| using namespace std; |
| |
| struct TreeNode { |
| int val; |
| TreeNode *left; |
| TreeNode *right; |
| |
| TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| }; |
| |
| class Codec { |
| public: |
| int cnt; |
| |
| void encode(TreeNode *root, string &str) { |
| if (root == nullptr) { |
| str.append("#,"); |
| return; |
| } |
| str.append(to_string(root->val) + ","); |
| encode(root->left, str); |
| encode(root->right, str); |
| } |
| |
| TreeNode *decode(vector<string> &vals) { |
| string cur = vals[cnt++]; |
| if (cur == "#") return nullptr; |
| TreeNode *node = new TreeNode(stoi(cur)); |
| node->left = decode(vals); |
| node->right = decode(vals); |
| return node; |
| } |
| |
| string serialize(TreeNode *root) { |
| string res = ""; |
| encode(root, res); |
| return res; |
| } |
| |
| TreeNode *deserialize(string data) { |
| vector<string> vals = split(data, ","); |
| cnt = 0; |
| return decode(vals); |
| } |
| |
| vector<string> split(const string &str, const string &split) { |
| vector<string> res; |
| |
| regex reg(split); |
| sregex_token_iterator pos(str.begin(), str.end(), reg, -1); |
| for (decltype(pos) end; pos != end; ++pos) |
| res.push_back(pos->str()); |
| return res; |
| } |
| }; |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <regex> |
| #include <iostream> |
| #include <queue> |
| |
| using namespace std; |
| |
| struct TreeNode { |
| int val; |
| TreeNode *left; |
| TreeNode *right; |
| |
| TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| }; |
| |
| class Codec { |
| public: |
| |
| void encode(TreeNode *root, string &str) { |
| queue<TreeNode *> q; |
| q.emplace(root); |
| |
| while (!q.empty()) { |
| TreeNode *cur = q.front(); |
| q.pop(); |
| if (cur == nullptr) { |
| str.append("#,"); |
| } else { |
| str.append(to_string(cur->val) + ","); |
| |
| q.emplace(cur->left); |
| q.emplace(cur->right); |
| } |
| } |
| } |
| |
| TreeNode *generate(string val) { |
| return val == "#" ? nullptr : new TreeNode(stoi(val)); |
| } |
| |
| TreeNode *decode(vector<string> &vals) { |
| int cnt = 0; |
| TreeNode *root = generate(vals[cnt++]); |
| queue<TreeNode *> q; |
| q.emplace(root); |
| |
| while (!q.empty()) { |
| TreeNode *node = q.front(); |
| q.pop(); |
| if (node == nullptr) continue; |
| node->left = generate(vals[cnt++]); |
| node->right = generate(vals[cnt++]); |
| if (node->left != nullptr) q.emplace(node->left); |
| if (node->right != nullptr) q.emplace(node->right); |
| } |
| return root; |
| } |
| |
| string serialize(TreeNode *root) { |
| string res = ""; |
| encode(root, res); |
| return res; |
| } |
| |
| TreeNode *deserialize(string data) { |
| vector<string> vals = split(data, ","); |
| return decode(vals); |
| } |
| |
| vector<string> split(const string &str, const string &split) { |
| vector<string> res; |
| |
| regex reg(split); |
| sregex_token_iterator pos(str.begin(), str.end(), reg, -1); |
| for (decltype(pos) end; pos != end; ++pos) |
| res.push_back(pos->str()); |
| return res; |
| } |
| }; |
-
todo
-
利用先序与中序遍历序列构造二叉树
-
无重复值
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <regex> |
| #include <iostream> |
| #include <stack> |
| |
| 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 *buildTree(vector<int> &preorder, vector<int> &inorder) { |
| if (preorder.size() == 0) return nullptr; |
| int pre = 0; |
| int in = 0; |
| stack<TreeNode *> stk; |
| |
| TreeNode *curRoot = new TreeNode(preorder[pre++]); |
| TreeNode *res = curRoot; |
| stk.emplace(curRoot); |
| |
| for (; pre < preorder.size(); pre++) { |
| if (curRoot->val == inorder[in]) { |
| |
| while (!stk.empty() && stk.top()->val == inorder[in]) { |
| curRoot = stk.top(); |
| stk.pop(); |
| in++; |
| } |
| TreeNode *node = new TreeNode(preorder[pre]); |
| curRoot->right = node; |
| curRoot = curRoot->right; |
| stk.emplace(curRoot); |
| } else { |
| |
| TreeNode *node = new TreeNode(preorder[pre]); |
| curRoot->left = node; |
| |
| curRoot = curRoot->left; |
| stk.emplace(curRoot); |
| } |
| } |
| return res; |
| } |
| }; |
| #include <vector> |
| #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: |
| int pre; |
| int in; |
| |
| TreeNode *generate(vector<int> &preorder, vector<int> &inorder, int stop) { |
| if (pre == preorder.size()) return nullptr; |
| if (inorder[in] == stop) { |
| |
| in++; |
| return nullptr; |
| } |
| int rootVal = preorder[pre++]; |
| TreeNode *root = new TreeNode(rootVal); |
| |
| root->left = generate(preorder, inorder, rootVal); |
| |
| root->right = generate(preorder, inorder, stop); |
| return root; |
| } |
| |
| TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { |
| pre = 0; |
| in = 0; |
| return generate(preorder, inorder, INT_MAX); |
| } |
| }; |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <regex> |
| #include <iostream> |
| #include <stack> |
| #include <unordered_map> |
| |
| 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: |
| unordered_map<int, int> map; |
| |
| TreeNode *generate(vector<int> &preorder, int rootIndex, int left, int right) { |
| if (left > right) return nullptr; |
| TreeNode *node = new TreeNode(preorder[rootIndex]); |
| |
| int in = map[preorder[rootIndex]]; |
| |
| |
| node->left = generate(preorder, rootIndex + 1, left, in - 1); |
| |
| |
| node->right = generate(preorder, rootIndex + in - left + 1, in + 1, right); |
| return node; |
| } |
| |
| TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { |
| |
| for (int i = 0; i < inorder.size(); i++) |
| map[inorder[i]] = i; |
| return generate(preorder, 0, 0, inorder.size() - 1); |
| } |
| }; |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <regex> |
| #include <iostream> |
| #include <stack> |
| #include <queue> |
| |
| 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 isCompleteTree(TreeNode *root) { |
| if (root == nullptr) return true; |
| queue<TreeNode *> q; |
| q.emplace(root); |
| |
| bool leaf = false; |
| while (!q.empty()) { |
| TreeNode *node = q.front(); |
| q.pop(); |
| |
| if (leaf == true |
| && (node->left != nullptr || node->right != nullptr)) |
| return false; |
| |
| if (node->left == nullptr && node->right != nullptr) return false; |
| |
| if (node->left != nullptr) q.emplace(node->left); |
| if (node->right != nullptr) q.emplace(node->right); |
| |
| if (node->left == nullptr || node->right == nullptr) leaf = true; |
| } |
| return true; |
| } |
| }; |
| #include <vector> |
| #include <algorithm> |
| #include <string> |
| #include <regex> |
| #include <iostream> |
| #include <stack> |
| #include <queue> |
| #include <valarray> |
| |
| 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 getDepth(TreeNode *root) { |
| int depth = 0; |
| while (root != nullptr) { |
| depth++; |
| root = root->left; |
| } |
| return depth; |
| } |
| |
| |
| int countNodes(TreeNode *root) { |
| if (root == nullptr) return 0; |
| int leftDepth = getDepth(root->left); |
| int rightDepth = getDepth(root->right); |
| int leftCnt = 0; |
| int rightCnt = 0; |
| |
| if (leftDepth == rightDepth) { |
| |
| leftCnt = pow(2, leftDepth) - 1; |
| |
| rightCnt = countNodes(root->right); |
| } else { |
| |
| rightCnt = pow(2, rightDepth) - 1; |
| |
| leftCnt = countNodes(root->left); |
| } |
| |
| return leftCnt + rightCnt + 1; |
| } |
| }; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步