目录
Leetcode-98
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode* root) {
return dfs(root, INT_MIN, INT_MAX);
}
bool dfs(TreeNode *root, long long minv, long long maxv){
if(!root) return true;
if(root -> val < minv || root -> val > maxv) return false;
return dfs(root -> left, minv, root -> val -1ll) && dfs(root -> right, root -> val + 1ll, maxv);
}
};
Leetcode-94
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode*> stk;
vector<int> res;
if(!root) return res;
auto p = root;
while(p || stk.size()){
while(p){
stk.push(p);
p = p -> left;
}
p = stk.top();
stk.pop();
res.push_back(p -> val);
p = p -> right;
}
return res;
}
};
Leetcode-101
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// 递归
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
return dfs(root -> left, root -> right);
}
bool dfs(TreeNode *p, TreeNode * q){
if(!p || !q) return !p && !q;
return p -> val == q -> val && dfs(p -> left, q -> right) && dfs(p -> right, q -> left);
}
};
// 迭代
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
stack<TreeNode*> left, right;
auto l = root -> left, r = root -> right;
while(l || r || left.size() || right.size()){
while(l && r){
left.push(l), l = l -> left;
right.push(r), r = r -> right;
}
if(l || r)
return false;
l = left.top(), left.pop();
r = right.top(), right.pop();
if(l -> val != r -> val)
return false;
l = l -> right, r = r -> left;
}
return true;
}
};
Leetcode-105
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
unordered_map<int, int> pos; // 建立哈希表
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
int n = preorder.size();
for(int i = 0; i < n; i ++)
pos[inorder[i]] = i;
return dfs(preorder, inorder, 0, n - 1, 0, n - 1);
}
TreeNode* dfs(vector<int> &preorder, vector<int> &inorder, int pl, int pr, int il,int ir){
if(pl > pr) return NULL;
int val = preorder[pl];
int k = pos[val];
int len = k - il;
auto root = new TreeNode(val);
root -> left = dfs(preorder, inorder, pl + 1, pl + len, il, k - 1);
root -> right = dfs(preorder, inorder, pl + len + 1, pr, k + 1, ir);
return root;
}
};
Leetcode-102
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
while(q.size()){
int len = q.size();
vector<int> level;
for(int i = 0; i < len; i++){
auto t =q.front();
q.pop();
level.push_back(t -> val);
if(t -> left) q.push(t -> left);
if(t -> right) q.push(t -> right);
}
res.push_back(level);
}
return res;
}
};
Leetcode-236
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root || root == p || root == q) return root;
auto left = lowestCommonAncestor(root -> left, p, q);
auto right = lowestCommonAncestor(root -> right, p, q);
if(!left) return right;
if(!right) return left;
return root;
}
};
// class Solution{
// public:
// TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode *p, TreeNode * q){
// std::vector<TreeNode*> path;
// std::vector<TreeNode*> node_p_path;
// std::vector<TreeNode*> node_q_path;
// int finish = 0;
// preorder(root, p, path, node_p_path, finish);
// path.clear();
// finish = 0;
// preorder(root, q, path, node_q_path, finish);
// int path_len = 0;
// if(node_p_path.size() < node_q_path.size()){
// path_len = node_p_path.size();
// }
// else{
// path_len = node_q_path.size();
// }
// TreeNode *result = 0;
// for(int i=0; i<path_len; i++){
// if(node_p_path[i] == node_q_path[i]){
// result = node_p_path[i];
// }
// }
// return result;
// }
// private:
// void preorder(TreeNode *node, TreeNode* search,std::vector<TreeNode*> &path,
// std::vector<TreeNode*> &result, int &finish){
// if(!node || finish){
// return;
// }
// path.push_back(node);
// if(node == search){
// finish = 1;
// result = path;
// }
// preorder(node->left, search, path, result, finish);
// preorder(node->right,search, path, result, finish);
// path.pop_back();
// }
// };
Leetcode-543
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int ans = 0;
int diameterOfBinaryTree(TreeNode* root) {
dfs(root);
return ans;
}
int dfs(TreeNode* root){
if(!root) return 0;
auto left = dfs(root -> left);
auto right = dfs(root -> right);
ans = max(ans, left + right);;
return max(left + 1, right + 1);
}
};
Leetcode-124
/** Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int ans = INT_MIN;
int maxPathSum(TreeNode* root) {
dfs(root);
return ans;
}
int dfs(TreeNode* root){
if(!root) return 0;
auto left = dfs(root -> left);
auto right = dfs(root -> right);
ans = max(ans, left + right + root -> val);
return max(0, root -> val + max(left, right));
}
};
Leetcode-173
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class BSTIterator {
public:
stack<TreeNode*> stk;
BSTIterator(TreeNode* root) {
while(root){
stk.push(root);
root = root -> left;
}
}
/** @return the next smallest number */
int next() {
auto p = stk.top();
stk.pop();
int res = p -> val;
p = p -> right;
while(p){
stk.push(p);
p = p -> left;
}
return res;
}
/** @return whether we have a next smallest number */
bool hasNext() {
return !stk.empty();
}
};
/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator* obj = new BSTIterator(root);
* int param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/
Leetcode-297
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string res;
dfs1(root, res); // 用传引用的方式,以减小时间复杂度
return res;
}
void dfs1(TreeNode* root, string &res){
if(!root){
res += "#,";
return;
}
res += to_string(root -> val) + ',';
dfs1(root -> left, res);
dfs1(root -> right, res);
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
int u = 0;
return dfs2(data, u);
}
TreeNode* dfs2(string &data, int &u){
if(data[u] == '#'){
u += 2;
return NULL;
}
int t = 0;
bool is_minus = false; // 判断是否是负数
if(data[u] == '-'){
is_minus = true;
u++;
}
while(data[u] != ','){
t = t * 10 + data[u] - '0';
u++;
}
u++;
if(is_minus) t = -t;
auto root = new TreeNode(t);
root -> left = dfs2(data, u);
root -> right = dfs2(data, u);
return root;
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));