Leetcode94. 二叉树的中序遍历
/**
* 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> res;
vector<int> inorderTraversal(TreeNode* root) {
dfs(root);
return res;
}
void dfs(TreeNode* root) {
if (root == nullptr) return ;
dfs(root->left);
res.push_back(root->val);
dfs(root->right);
}
};
Leetcode96. 不同的二叉搜索树
思路
枚举根结点,不断累加左子树个数*右子树的个数。不同二叉搜索树,只要长度相同,二叉搜索树的个数就是一样的,因此可以从前向后递推
class Solution {
public:
int numTrees(int n) {
vector<int> f(n + 1);
f[0] = 1;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= i; j ++ )
f[i] += f[j - 1] * f[i - j];
return f[n];
}
};
Leetcode98. 验证二叉搜索树
思路一:中序遍历有序
空结点说明当前状态有序的,判断一下中序遍历前一个结点是否比当前结点的值要小,如果不符合返回falsefalsefalse,将前驱结点更新,遍历右子树
/**
* 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:
long long pre = -1e18;
bool isValidBST(TreeNode* root) {
if (root == nullptr) return true;
if (!isValidBST(root->left)) return false;
if (root->val <= pre) return false;
pre = root->val;
return isValidBST(root->right);
}
};
思路二:根据定义判断每一个结点是否满足BST的性质
/**
* 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, LONG_MIN, LONG_MAX);
}
bool dfs(TreeNode* root, long l, long r) {
if (!root) return true;
if (root->val <= l || root->val >= r) return false;
return dfs(root->left, l, root->val) && dfs(root->right, root->val, r);
}
};
Leetcode101. 对称二叉树
思路:递归
/**
* 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;
else return dfs(root->left, root->right);
}
bool dfs(TreeNode* l, TreeNode* r) {
if (!l && !r) return true;
if (!l || !r || l->val != r->val) return false;
return dfs(l->left, r->right) && dfs(l->right, r->left);
}
};
Leetcode102. 二叉树的层序遍历
/**
* 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;
queue<TreeNode*> q;
if (root) q.push(root);
while (q.size()) {
vector<int> level;
int len = q.size();
while (len -- ) {
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;
}
};
Leetcode104. 二叉树的最大深度
思路一:DFS求根节点到叶子结点最大距离
/**
* 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:
int maxDepth(TreeNode* root) {
if (!root) return 0;
else return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
思路二:DFS求每个结点到根节点的距离
额外传递一个参数,代表每一层的深度
/**
* 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:
int res = 0;
int maxDepth(TreeNode* root) {
if (!root) return 0;
dfs(root, 0);
return res;
}
void dfs(TreeNode* root, int depth) {
if (!root) {
res = max(res, depth);
return ;
}
dfs(root->left, depth + 1);
dfs(root->right, depth + 1);
}
};
本文介绍了LeetCode中的四个经典问题:二叉树的中序遍历、不同二叉搜索树的计数、验证二叉搜索树和对称二叉树。通过递归和迭代方式实现解决方案,并探讨了思路和关键代码。
8万+

被折叠的 条评论
为什么被折叠?



