226. 翻轉二叉樹
題目連結:https://leetcode.cn/problems/invert-binary-tree/
題目難度:簡單
文章講解:https://programmercarl.com/0226.翻轉二叉樹.html
影片講解:https://www.bilibili.com/video/BV1sP4y1f7q7
題目狀態:透過
個人思路:
類似二叉樹的層序遍歷的變形,建立一個佇列,先將根節點壓入佇列中,當該節點壓出佇列時,若該節點有左右孩子,將該節點的左右孩子進行翻轉(swap
),並將其左右孩子節點壓入佇列中,直到佇列中沒有節點了,整個迴圈結束,二叉樹翻轉完成。
程式碼實現:
/**
* 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:
TreeNode* invertTree(TreeNode* root) {
if(!root) return nullptr;
queue<TreeNode *> que;
que.push(root);
while(!que.empty()) {
TreeNode *node = que.front();
que.pop();
swap(node->left, node->right);
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
return 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:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
return 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:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
invertTree(root->left);
invertTree(root->right);
swap(root->left, root->right);
return 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:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
invertTree(root->left);
swap(root->left, root->right);
invertTree(root->left);
return root;
}
};
101. 對稱二叉樹
題目連結:https://leetcode.cn/problems/symmetric-tree/
題目難度:簡單
文章講解:https://programmercarl.com/0101.對稱二叉樹.html
影片講解:https://www.bilibili.com/video/BV1ue4y1Y7Mf
題目狀態:沒思路
思路:
使用遞迴,分別遍歷其左孩子是否等於右孩子,若相等,在遍歷其左孩子的左孩子是否等於其右孩子的右孩子,以及遍歷其左孩子的右孩子是否等於其右孩子的左孩子,若一直進行下去且遍歷完成,表示該二叉樹是一個對稱二叉樹。
程式碼實現:
/**
* 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 equal(TreeNode *left, TreeNode *right) {
if(left == nullptr && right != nullptr) return false;
else if(left != nullptr && right == nullptr) return false;
else if(left == nullptr && right == nullptr) return true;
else if(left->val != right->val) return false;
bool outside = equal(left->left, right->right);
bool inside = equal(left->right, right->left);
bool isSame = outside && inside;
return isSame;
}
bool isSymmetric(TreeNode* root) {
if(!root) return true;
return equal(root->left, root->right);
}
};
使用迭代方法程式碼實現(使用兩個佇列,棧也可以,只需將下面程式碼中的兩個佇列改為兩個棧即可):
/**
* 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 == nullptr) return true;
queue<TreeNode *> que;
que.push(root->left);
que.push(root->right);
while(!que.empty()) {
TreeNode *leftNode = que.front(); que.pop();
TreeNode *rightNode = que.front(); que.pop();
if(!leftNode && !rightNode) continue;
if(!leftNode || !rightNode || (leftNode->val != rightNode->val)) return false;
que.push(leftNode->left);
que.push(rightNode->right);
que.push(leftNode->right);
que.push(rightNode->left);
}
return true;
}
};
100. 相同的樹
題目連結:https://leetcode.cn/problems/same-tree/
題目難度:簡單
題目狀態:透過
思路和上一題的思路是一樣的,直接看程式碼:
程式碼實現:
/**
* 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 == nullptr && q != nullptr) return false;
else if(p != nullptr && q == nullptr) return false;
else if(p == nullptr && q == nullptr) return true;
else if(p->val != q->val) return false;
bool leftEqual = isSameTree(p->left, q->left);
bool rightEqual = isSameTree(p->right, q->right);
bool isSame = leftEqual && rightEqual;
return isSame;
}
};