class Solution {
vector<int> res;
public:
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);
}
};
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 + 1 : right + 1;
}
};
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr) return nullptr;
TreeNode *left = invertTree(root->left);
TreeNode *right = invertTree(root->right);
root->left = right;
root->right = left;
return root;
}
};
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return dfs(root->left, root->right);
}
bool dfs(TreeNode* left, TreeNode* right)
{
if (left && right)
{
if (left->val != right->val) return false;
return dfs(left->left, right->right) && dfs(left->right, right->left);
}
else if (left != right) return false;
else return true;
}
};
class Solution {
int depth;
public:
int diameterOfBinaryTree(TreeNode* root) {
dfs(root);
return depth - 1;
}
int dfs(TreeNode* root)
{
if (root == nullptr) return 0;
int left = dfs(root->left);
int right = dfs(root->right);
depth = max(depth, left + right + 1);
return max(left, right) + 1;
}
};
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode*> q;
if (root == nullptr) return res;
q.push(root);
while (q.size())
{
int sz = q.size();
vector<int> tmp;
while (sz--)
{
TreeNode *node = q.front();
tmp.push_back(node->val);
q.pop();
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
res.push_back(tmp);
}
return res;
}
};
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
return dfs(nums, 0, nums.size() - 1);
}
TreeNode* dfs(vector<int>& nums, int l, int r)
{
if (l > r) return nullptr;
int mid = l + (r - l) / 2;
TreeNode* node = new TreeNode(nums[mid]);
node->left = dfs(nums, l, mid - 1);
node->right = dfs(nums, mid + 1, r);
return node;
}
};
递归遍历。
class Solution {
public:
bool isValidBST(TreeNode* root) {
return dfs(root, LONG_MIN, LONG_MAX);
}
bool dfs(TreeNode* root, long min_val, long max_val)
{
if (root == nullptr) return true;
if (root->val <= min_val || root->val >= max_val) return false;
return dfs(root->left, min_val, root->val)
&& dfs(root->right, root->val, max_val);
}
};
前序遍历。
class Solution {
long prev = LONG_MIN;
public:
bool isValidBST(TreeNode* root) {
if (root == nullptr) return true;
if (isValidBST(root->left) == false) return false;
if (root->val <= prev) return false;
prev = root->val;
return isValidBST(root->right);
}
};
class Solution {
int res, cnt;
public:
int kthSmallest(TreeNode* root, int k) {
cnt = k;
dfs(root);
return res;
}
void dfs(TreeNode* root)
{
if (root == nullptr) return;
dfs(root->left);
if (--cnt == 0)
{
res = root->val;
return;
}
dfs(root->right);
}
};
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有