Created
December 3, 2018 07:01
-
-
Save XcqRomance/c40696b20445e12ff97ad5de32fdb2c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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