0.引言
● 层序遍历 10
● 226.翻转二叉树
● 101.对称二叉树 2
1.翻转二叉树
Category | Difficulty | Likes | Dislikes |
---|---|---|---|
algorithms | Easy (79.52%) | 1525 | - |
给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
示例 1:
输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]
示例 2:
输入:root = [2,1,3]
输出:[2,3,1]
示例 3:
输入:root = []
输出:[]
提示:
- 树中节点数目范围在
[0, 100]
内 -100 <= Node.val <= 100
1.1递归法
/*
* @lc app=leetcode.cn id=226 lang=cpp
*
* [226] 翻转二叉树
*/
// @lc code=start
/**
* 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) {
dfs(root);
return root;
}
// 前序遍历
private:
void dfs(TreeNode* node) {
if (node == nullptr) return;
std::swap(cur->left, cur->right);
dfs(node->left);
dfs(node->right);
}
};
// @lc code=end
1.2.迭代法
/*
* @lc app=leetcode.cn id=226 lang=cpp
*
* [226] 翻转二叉树
*/
// @lc code=start
/**
* 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) {
std::stack<TreeNode*> node_stack;
if (root == nullptr) return root;
node_stack.push(root);
while (!node_stack.empty()) {
TreeNode* cur = node_stack.top();
node_stack.pop();
std::swap(cur->left, cur->right);
if (cur->left) node_stack.push(cur->left);
if (cur->right) node_stack.push(cur->right);
}
return root;
}
};
// @lc code=end
1.3.层序遍历BFS
/*
* @lc app=leetcode.cn id=226 lang=cpp
*
* [226] 翻转二叉树
*/
// @lc code=start
/**
* 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) {
std::queue<TreeNode*> que;
if (root != nullptr) {
que.push(root);
}
while (!que.empty()) {
for (int i = que.size(); i > 0; --i) {
TreeNode* cur = que.front();
que.pop();
std::swap(cur->left, cur->right);
if (cur->left) que.push(cur->left);
if (cur->right) que.push(cur->right);
}
}
return root;
}
};
// @lc code=end
2.对称二叉树
Category | Difficulty | Likes | Dislikes |
---|---|---|---|
algorithms | Easy (58.64%) | 2320 | - |
给你一个二叉树的根节点 root
, 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3]
输出:true
示例 2:
输入:root = [1,2,2,null,3,null,3]
输出:false
提示:
- 树中节点数目在范围
[1, 1000]
内 -100 <= Node.val <= 100
进阶:你可以运用递归和迭代两种方法解决这个问题吗?
2.1.递归法
/*
* @lc app=leetcode.cn id=101 lang=cpp
*
* [101] 对称二叉树
*/
// @lc code=start
/**
* 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) { return dfs(root, root); }
// 后续遍历
private:
bool dfs(TreeNode* left, TreeNode* right) {
if (left == nullptr && right == nullptr) return true;
if (left == nullptr || right == nullptr) return false;
if (left->val != right->val) return false;
bool out_side = dfs(left->left, right->right);
bool in_side = dfs(left->right, right->left);
return in_side && out_side;
}
};
// @lc code=end
2.2.迭代法-队列
/*
* @lc app=leetcode.cn id=101 lang=cpp
*
* [101] 对称二叉树
*/
// @lc code=start
/**
* 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;
std::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 == nullptr && right == nullptr) continue;
if (left == nullptr || right == nullptr) return false;
if (left->val != right->val) return false;
// 外侧
que.push(left->left);
que.push(right->right);
// 内侧
que.push(left->right);
que.push(right->left);
}
return true;
}
};
// @lc code=end
2.3.迭代法-栈
/*
* @lc app=leetcode.cn id=101 lang=cpp
*
* [101] 对称二叉树
*/
// @lc code=start
/**
* 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;
std::stack<TreeNode*> st;
st.push(root->left);
st.push(root->right);
while (!st.empty()) {
TreeNode* left = st.top();
st.pop();
TreeNode* right = st.top();
st.pop();
// 判断逻辑,与递归逻辑相同
if (left == nullptr && right == nullptr) continue;
if (left == nullptr || right == nullptr) return false;
if (left->val != right->val) return false;
// 外侧
st.push(left->left);
st.push(right->right);
// 内侧
st.push(left->right);
st.push(right->left);
}
return true;
}
};
// @lc code=end