2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Binary tree chapter, start practicing binary tree operations.
Record forty-two [101. Symmetric binary tree].
continue.
Given a binary tree root, Check if it is axisymmetric。
Example 1:
输入:root = [1,2,2,3,4,4,3]
输出:true
Example 2:
输入:root = [1,2,2,null,3,null,3]
输出:false
hint:
树中节点数目在范围 [1, 1000] 内
-100 <= Node.val <= 100
Advanced: You can useRecursion and IterationAre there two ways to solve this problem?
Let’s try it with recursion first. Core: What is the criterion for determining axial symmetry?
Recursion: calling itself.No duplicate logic has been found yet:
Change the iterative method to use level-order traversal.
After obtaining the layer sequence result, we determine whether each layer is an even number and is equal after reverse. However, this is not true for Example 2 because the null pointer is excluded.
The idea doesn't work.
Summarize:Although we know that we need to find symmetry, we don’t know how to unify the criteria for judging symmetry.。
Every comment is an idea.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool iscompare(TreeNode* left,TreeNode* right){ //两边同时遍历,所以两个参数。返回是否相等,用bool类型。
//确定终止条件
if(!left && !right) return true; //同时为空,可以翻转
else if(!left && right) return false; //一个空,一个不为空。肯定不等
else if (!right && left) return false;//一个空,一个不为空。肯定不等
else if(left->val != right->val) return false;//都不为空,但是值不等
//都不为空,值相等。说明可以继续进行外侧比较、内侧比较,不用return。
bool outside = iscompare(left->left,right->right); //同时比较,解决了左右遍历顺序不一样
bool inside = iscompare(left->right,right->left);
return outside && inside; //同时返回true。才能返回true
}
bool isSymmetric(TreeNode* root) {
return iscompare(root->left,root->right);
}
};
The traversal order of a tree (left subtree) is left-right-middle, and the traversal order of a tree (right subtree) is right-left-middle.
The stack structure is the same.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) return false;
queue<TreeNode*> que;
que.push(root->left);//放入左子树
que.push(root->right);//放入右子树
while(!que.empty()){
TreeNode* left = que.front(); que.pop();//取出比较对象中的左节点
TreeNode* right = que.front();que.pop();//取出比较对象中的右节点
if(!left && !right){ //都是空节点
continue;
}else if(!left || !right || left->val != right->val){
return false;
}
que.push(left->left);
que.push(right->right);
que.push(left->right);
que.push(right->left);
}
return true;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
vector<vector<int>> level;
queue<TreeNode*> que;
if(!root) return false;
que.push(root);
while(!que.empty()){
int size = que.size();
vector<int> vec;
while(size--){
TreeNode* cur = que.front();que.pop();
if(cur){ //不是空节点
que.push(cur->left);
que.push(cur->right);
vec.push_back(cur->val);
}else{
vec.push_back(INT_MIN);//因为节点的值【-100,100】。用一个最小值代表空。
}
}
level.push_back(vec);
}
//获得层序遍历。包含空。空的数值借助INT_MIN代替。
for(int i = 1;i < level.size();i++){
vector<int> temp = level[i];
reverse(temp.begin(),temp.end());
if(temp != level[i]){
return false;
}
}
return true;
}
};
Given two binary trees with roots p and q, write a function to test whether the two trees are identical.
If two treesAre structurally identical and the nodes have the same values, they are considered to be the same.
Example 1:
输入:p = [1,2,3], q = [1,2,3]
输出:true
Example 2:
输入:p = [1,2], q = [1,null,2]
输出:false
Example 3:
输入:p = [1,2,1], q = [1,1,2]
输出:false
hint:
两棵树上的节点数目都在范围 [0, 100] 内
-10^4 <= Node.val <= 10^4
Determine whether two trees are identical. They have the same structure and the same values.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(!p && !q) return true; //传入节点同时为空,可以对应
else if(!p && q) return false;//一个空,另一个不是空。不可能对应。
else if(p && !q) return false;//一个空,另一个不是空。不可能对应。
else if(p->val != q->val) return false;//值不等,不可能对应。
bool leftchild = isSameTree(p->left,q->left);
bool rightchild = isSameTree(p->right,q->right);
return leftchild && rightchild;
}
};
Given two binary trees root and subRoot. Check whether root contains the same tree as subRoot.Same structure and node valuesIf the subtree exists, return true; otherwise, return false.
A subtree of a binary tree tree includes a node of tree and all the descendant nodes of this node. tree can also be regarded as a subtree of itself.
Example 1:
输入:root = [3,4,5,1,2], subRoot = [4,1,2]
输出:true
Example 2:
输入:root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
输出:false
hint:
root 树上的节点数量范围是 [1, 2000]
subRoot 树上的节点数量范围是 [1, 1000]
-10^4 <= root.val <= 10^4
-10^4 <= subRoot.val <= 10^4
(1) Subtrees also determine whether two trees are equal. You can use [Problem 100. Code Implementation] to solve the equality judgment.
(2) But it must be found in the rootEqual to the subRoot root node valueThe node is used as the root node of the subtree. Use level-order traversal to traverse the root.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSame(TreeNode* rootnode,TreeNode* subRootnode){
if(!rootnode && !subRootnode) return true;
else if(!rootnode && subRootnode) return false;
else if(rootnode && !subRootnode) return false;
else if(rootnode->val != subRootnode->val) return false;
bool leftchild = isSame(rootnode->left,subRootnode->left);
bool rightchild = isSame(rootnode->right,subRootnode->right);
return leftchild && rightchild;
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
//先找到和subRoot值相等的节点,才有可能相等。得遍历root找到和subRoot值相等的节点,可能作为子树的根节点
//用层序遍历
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
int size = que.size();
while(size--){
TreeNode* cur = que.front();que.pop();
if(cur->val == subRoot->val){
bool subtree = isSame(cur,subRoot);
if(subtree) return true;
}
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return false;
}
};
Core: Traverse two trees at the same time to determine the comparison object. Implement recursion.
Going deep into any tree traversal will not yield both sides of the comparison object.
(Corrections are welcome, please indicate the source when reprinting)