目录
965. 单值二叉树
100. 相同的树
101. 对称二叉树
144. 二叉树的前序遍历
94. 二叉树的中序遍历
145. 二叉树的后序遍历
572. 另一棵树的子树
bool isEqualToVal(struct TreeNode* root, int val)
{
if (root == NULL)
return true;
if (root->val != val)
return false;
return isEqualToVal(root->left, val) && isEqualToVal(root->right, val);
}
bool isUnivalTree(struct TreeNode* root)
{
if (root == NULL)
return true;
return isEqualToVal(root->left, root->val) && isEqualToVal(root->right, root->val);
}
思路:遍历二叉树,判断左右子树中的每个结点是否和根结点具有相同的值。
bool isSameTree(struct TreeNode* p, struct TreeNode* q)
{
if (p == NULL && q == NULL) // 若 p 和 q 皆为空
return true;
if (p == NULL || q == NULL) // 若 p 或 q 为空
return false;
// 若 p 和 q 都不为空
if (p->val != q->val)
return false;
else
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
bool isSymmetricTree(struct TreeNode* p, struct TreeNode* q)
{
if (p == NULL && q == NULL)
return true;
if (p == NULL || q == NULL)
return false;
if (p->val != q->val)
return false;
else
return isSymmetricTree(p->left, q->right) && isSymmetricTree(p->right, q->left);
}
bool isSymmetric(struct TreeNode* root)
{
if (root == NULL)
return true;
return isSymmetricTree(root->left, root->right);
}
int BiTNodeCount(struct TreeNode* root)
{
if (root == NULL)
return 0;
else
return 1 + BiTNodeCount(root->left) + BiTNodeCount(root->right);
}
void PreOrder(struct TreeNode* root, int* ans, int* pi)
{
if (root == NULL)
return;
ans[(*pi)++] = root->val;
PreOrder(root->left, ans, pi);
PreOrder(root->right, ans, pi);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
*returnSize = BiTNodeCount(root);
int* ans = (int*)malloc(*returnSize * sizeof(int));
int i = 0;
PreOrder(root, ans, &i);
return ans;
}
int BiTNodeCount(struct TreeNode* root)
{
if (root == NULL)
return 0;
else
return 1 + BiTNodeCount(root->left) + BiTNodeCount(root->right);
}
void InOrder(struct TreeNode* root, int* ans, int* pi)
{
if (root == NULL)
return;
InOrder(root->left, ans, pi);
ans[(*pi)++] = root->val;
InOrder(root->right, ans, pi);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize)
{
*returnSize = BiTNodeCount(root);
int* ans = (int*)malloc(*returnSize * sizeof(int));
int i = 0;
InOrder(root, ans, &i);
return ans;
}
int BiTNodeCount(struct TreeNode* root)
{
if (root == NULL)
return 0;
else
return 1 + BiTNodeCount(root->left) + BiTNodeCount(root->right);
}
void PostOrder(struct TreeNode* root, int* ans, int* pi)
{
if (root == NULL)
return;
PostOrder(root->left, ans, pi);
PostOrder(root->right, ans, pi);
ans[(*pi)++] = root->val;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize)
{
*returnSize = BiTNodeCount(root);
int* ans = (int*)malloc(*returnSize * sizeof(int));
int i = 0;
PostOrder(root, ans, &i);
return ans;
}
bool isSameTree(struct TreeNode* p, struct TreeNode* q)
{
if (p == NULL && q == NULL)
return true;
if (p == NULL || q == NULL)
return false;
if (p->val != q->val)
return false;
else
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot)
{
// 注意:subRoot 不为空
if (root == NULL)
return false;
return isSameTree(root, subRoot) ||
isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
}
思路:以每一个结点为根结点的子树和
subRoot
依次进行比较,判断是否为相同的树。