Binary tree basic OJ exercises
Article directory
1. The first question: 965. Single-valued binary tree
1. Topic
Topic: as follows (example):
bool isUnivalTree(struct TreeNode* root)
{
}
2. Idea + source code
It is necessary to ensure that the left subtree of the roof is equal to the right subtree and equal to the root node (when the binary tree is empty, it also returns true)
The code is as follows (example):
bool isUnivalTree(struct TreeNode* root){
if(root == NULL)
return true;
if(root -> left && root -> left->val != root->val)
return false;
if(root -> right && root -> right -> val != root->val)
return false;
return isUnivalTree(root->left) && isUnivalTree(root->right);
}
3. Recursive Diagram
For convenience, we use the following figure for recursive expansion. For better understanding, I only draw the recursive figure of the left subtree. If you are interested, you can realize it yourself!
2. The second question: 100. The same tree
1. Topic
Topic: as follows (example):
bool isSameTree(struct TreeNode* p, struct TreeNode* q)
{
}
2. Idea + source code
(1). When both p and q are empty, return true
(2). When one of them is not empty, return false
(3). When both are empty, when the val of p is not equal to the val of q, return false . Come down and compare each node of p and q
through recursion ! As the following source code and graphic demonstration !
Note: The time complexity of this question is O(N) (this question is equivalent to traversing a binary tree so it is O(N))
The code is as follows (example):
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;
}
return isSameTree(p->left , q->left) && isSameTree(p->right , q->right);
}
3. Recursive Diagram
For convenience, we use the following diagram to recursively expand the diagram
3. Question 3: 101. Symmetric binary tree
1. Topic
Topic: as follows (example):
bool isSymmetric(struct TreeNode* root)
{
}
2. Idea + source code
For this question, we rewrite a sub-function :
1. If the root is NULL, it returns true directly (the empty tree is symmetrical)
2. When the left tree and the right tree of the roof are both NULL, it is true , and one of them is not NULL. It is false
3. Use sub-functions for recursion: compare the val values of the left subtree and the right subtree, if they are the same, continue to compare!
The code is as follows (example):
bool _isSymmetric(struct TreeNode* root1 , struct TreeNode* root2)
{
if(root1 == NULL && root2 == NULL)
{
return true;
}
if(root1 == NULL || root2 == NULL)
{
return false;
}
if(root1->val != root2->val)
{
return false;
}
return _isSymmetric(root1->left , root2->right)
&& _isSymmetric(root1->right, root2->left);
}
bool isSymmetric(struct TreeNode* root)
{
if(root == NULL)
{
return true;
}
return _isSymmetric(root->left , root ->right);
}
3. Recursive Diagram
For convenience, we use the following figure for recursive expansion. Here we only understand the recursive expansion of the sub-function . The main function only judges the case of an empty tree!
4. Question 4: 572. Subtree of another tree
1. Topic
Topic: as follows (example):
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot)
{
}
2. Idea + source code
1. If the root is NULL, it cannot be the same, return false 2. Use the function isSameTree function
we wrote before ( the third ) to judge whether it is correct 3. Keep recursing until the subtree of another tree is found ! Note : The best time complexity of this algorithm is: O(N) (find it after only one traversal); the worst: O(N^2) (traverse N times and find isSame N times)
The code is as follows (example):
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;
return isSameTree(p->left , q->left)
&& isSameTree(p->right , q->right);
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot)
{
if(root == NULL)
{
return false;
}
if(isSameTree(root , subRoot))
{
return true;
}
return isSubtree(root->left , subRoot)
|| isSubtree(root->right , subRoot);
}
3. Recursive Diagram
For convenience, we use the following figure to recursively expand the figure! Note: This tree structure is relatively simple. When we draw a recursive graph, we can draw it step by step from simple
Summarize
The above is the content of this article. This article explains in detail the 4 interview questions and diagrams related to the Leedcode binary tree!
If my blog is helpful to you, remember to support it three times, thank you for your support!