Skip to content

Instantly share code, notes, and snippets.

@XcqRomance
Created December 3, 2018 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XcqRomance/c40696b20445e12ff97ad5de32fdb2c5 to your computer and use it in GitHub Desktop.
Save XcqRomance/c40696b20445e12ff97ad5de32fdb2c5 to your computer and use it in GitHub Desktop.
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
// 二叉树的最大深度
int maxDepth(struct TreeNode* root) {
if (root) {
int left = 1;
int right = 1;
left += maxDepth(root->left);
right += maxDepth(root->right);
return left > right ? left : right;
} else {
return 0;
}
}
bool isValidBST(struct TreeNode* root) {
if (!root) {
return true;
} else {
// int left = lef
return isValidBST(root->left) && isValidBST(root->right);
}
return false;
}
bool myIsSymmetric(struct TreeNode *left,struct TreeNode *right) {
if (left == NULL && right == NULL) {
return true;
} else if (left == NULL || right == NULL) {
return false;
}
if (left->val != right->val) {
return false;
}
if (myIsSymmetric(left->left, right->right) == false || myIsSymmetric(left->right, right->left) == false) {
return false;
}
return true;//myIsSymmetric(left->left, right->right) && myIsSymmetric(left->right, right->left);
}
//对称二叉树
bool isSymmetric(struct TreeNode* root) {
if (root==NULL) {
return true;
}
return myIsSymmetric(root->left, root->right);
}
// 翻转二叉树 递归实现方式
struct TreeNode* invertTree(struct TreeNode* root) {
if (!root) {
return root;
}
if (!root->left && !root->right) {
return root;
}
struct TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
if (root->left) {
invertTree(root->left);
}
if (root->right) {
invertTree(root->right);
}
return root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment