LeetCode.树
LeetCode. 98
/**
* Definition for a binary tree node.
* 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 isValidBST(TreeNode* root) {
return dfs(root, INT_MIN, INT_MAX);
}
// 当前节点的值应该在[minv, maxv]这个值之间,如果符合分别递归判断左右子节点并且分别更新这个区间,否则return false
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() : 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<int> inorderTraversal(TreeNode* root) {
vector<int> ans;
stack<TreeNode*> stk;
auto p = root;
while(p || stk.size()) {
while(p) {
stk.push(p);
p = p->left;
}
p = stk.top();
stk.pop();
ans.push_back(p->val);
p = p->right;
}
return ans;
}
};
LeetCode. 1001 对称二叉树
递归:
/**
* Definition for a binary tree node.
* 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 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);
}
};
迭代:
/**
* Definition for a binary tree node.
* 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 isSymmetric(TreeNode* root) {
if(!root) return true;
stack<TreeNode*> left, right;
TreeNode *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() : 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 {
unordered_map<int, int> pos;
public:
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 nullptr;
int val = preorder[pl]; // 根节点的值
int k = pos[val]; // 根节点在中序遍历中的位置
int len = k - il; // 左子树的长度
TreeNode *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() : 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) {
vector<vector<int>> ans;
if(!root) return ans;
queue<TreeNode*> q;
q.push(root);
int len;
while(!q.empty()) {
len = q.size();
vector<int> level;
for(int i = 0; i< len; ++i) {
TreeNode* node = q.front();
q.pop();
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
level.push_back(node->val);
}
ans.push_back(level);
}
return ans;
}
};
LeetCode. 236 二叉树的最近公共祖先
递归中需要做的:
- 如果以root为根的子树中包含p和q,则返回p和q的最近公共祖先;
- 如果以root为根的子树中只包含q,则返回q;
- 如果以root为根的子树中只包含p,则返回p;
- 如果以root为根的子树中都不包含,则返回nullptr;
/**
* 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;
}
};
LeetCode. 543 二叉树的直径
枚举所有节点
/**
* Definition for a binary tree node.
* 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 {
int ans = 0;
public:
int diameterOfBinaryTree(TreeNode* root) {
dfs(root);
return ans;
}
int dfs(TreeNode *root) {
if(!root) return 0;
int left = dfs(root->left);
int right = dfs(root->right);
ans = max(ans, left+right);
return max(left,right)+1;
}
};
LeetCode. 124 二叉树的最大路径和
/**
* Definition for a binary tree node.
* 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 {
int ans = INT_MIN;
public:
int maxPathSum(TreeNode* root) {
dfs(root);
return ans;
}
// 返回的是权重的最大值
int dfs(TreeNode* root) {
if(!root) return 0;
int left = dfs(root->left);
int right = dfs(root->right);
ans = max(ans, root->val + left + right);
return max(0, root->val + max(0, max(left, right)));// 返回的时候,如果权重还没有0大,还不如不返回
}
};
LeetCode. 173 二叉搜索树迭代器
迭代法中序遍历二叉树的思想
/**
* Definition for a binary tree node.
* 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 BSTIterator {
stack<TreeNode*> stk;
public:
BSTIterator(TreeNode* root) {
while(root) {
stk.push(root);
root = root->left;
}
}
int next() {
TreeNode* node = stk.top();
stk.pop();
int tmp = node->val;
node = node->right;
while(node) {
stk.push(node);
node = node->left;
}
return tmp;
}
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 nullptr;
}
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;
TreeNode *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 ser, deser;
// TreeNode* ans = deser.deserialize(ser.serialize(root));
LeetCode. 106 从中序遍历和后序遍历序列构造二叉树
/**
* Definition for a binary tree node.
* 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 {
int len;
unordered_map<int, int> pos;
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
len = inorder.size() - 1;
for(int i = 0; i <= len; ++i) {
pos[inorder[i]] = i;
}
return dfs(inorder, postorder, 0, len, 0, len);
}
TreeNode* dfs(vector<int>& inorder, vector<int>& postorder, int il, int ir, int pl, int pr) {
if(pl > pr) return nullptr;
int num = postorder[pr];
int index = pos[num];
int length = ir - index;
TreeNode* node = new TreeNode(num);
node->right = dfs(inorder, postorder, index+1, ir, pr - length, pr - 1);
node->left = dfs(inorder, postorder, il, index-1, pl, pr - length - 1);
return node;
}
};