226.翻转二叉树
思路:交换每个节点的左右孩子即可,使用前序遍历和后序遍历比较好。
题解:
递归法前序遍历:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root == nullptr) return root;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
};
统一迭代法前序遍历
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
stack<TreeNode*> st;
if (root == nullptr) return root;
st.push(root);
while(!st.empty())
{
TreeNode *node = st.top();
if (node != nullptr)
{
st.pop();
if (node->right) st.push(node->right);
if (node->left) st.push(node->left);
st.push(node);
st.push(nullptr);
}
else
{
st.pop();
node = st.top();
swap(node->left, node->right);
st.pop();
}
}
return root;
}
};
层序迭代:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
queue<TreeNode*> que;
if (root != nullptr) que.push(root);
while(!que.empty())
{
TreeNode* node = que.front();
swap(node->left, node->right);
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
return root;
}
};
101. 对称二叉树
思路:采用后序遍历,对比左右子树。
题解:
递归法后序遍历:
class Solution {
public:
bool cmp(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;
return cmp(left->left, right->right) && cmp(left->right, right->left);
}
bool isSymmetric(TreeNode* root) {
if (root == nullptr) return true;
return cmp(root->left, root->right);
}
};
迭代法:
class Solution {
public:
bool isSymmetric(TreeNode* root) {
queue<TreeNode*> que;
if (root == nullptr) return true;
que.push(root->left);
que.push(root->right);
while(!que.empty())
{
TreeNode *l = que.front();
que.pop();
TreeNode *r = que.front();
que.pop();
if (!l && !r) continue;
if (!l || !r || l->val != r->val) return false;
que.push(l->left);
que.push(r->right);
que.push(l->right);
que.push(r->left);
}
return true;
}
};
104.二叉树的最大深度
思路:前序遍历求树的最大深度,后序遍历求根节点的高度(和树的最大深度相等),层序遍历的话是纯模板题。
题解:
层序遍历:
class Solution {
public:
int maxDepth(TreeNode* root) {
queue<TreeNode*> que;
int res{0};
if(root != nullptr) que.push(root);
while(!que.empty())
{
int size = que.size();
++res;
for (int i = 0; i < size; ++i)
{
TreeNode *node = que.front();
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return res;
}
};
递归前序遍历:
class Solution {
private:
int result = 0;
public:
void getDepth(TreeNode* node, int depth)
{
result = depth > result ? depth : result;
if (node->left == nullptr && node->right == nullptr) return;
if (node->left)
{
++depth;
getDepth(node->left, depth);
--depth;
}
if (node->right)
{
++depth;
getDepth(node->right, depth);
--depth;
}
return;
}
int maxDepth(TreeNode* root) {
if (root == nullptr) return 0;
getDepth(root, 1);
return result;
}
};
递归后序遍历:
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};
559. N 叉树的最大深度
思路:和二叉树的最大深度相同。
题解:
层序遍历:
class Solution {
public:
int maxDepth(Node* root) {
queue<Node*> que;
int res{0};
if(root != nullptr) que.push(root);
while(!que.empty())
{
int size = que.size();
++res;
for (int i = 0; i < size; ++i)
{
Node *node = que.front();
que.pop();
for (auto it : node->children)
que.push(it);
}
}
return res;
}
};
递归前序遍历:
class Solution {
private:
int result{0};
public:
void getDepth(Node *node, int depth)
{
result = depth > result ? depth : result;
if (node->children.empty()) return;
for (auto it : node->children)
{
++depth;
getDepth(it, depth);
--depth;
}
return;
}
int maxDepth(Node* root) {
if (root == nullptr) return 0;
getDepth(root, 1);
return result;
}
};
递归后序遍历:
class Solution {
public:
int getheight(Node *root)
{
if (root == nullptr) return 0;
int depth = 0;
for (auto it : root->children)
depth = max(getheight(it), depth);
return 1 + depth;
}
int maxDepth(Node* root) {
return getheight(root);
}
};
111.二叉树的最小深度
思路:层序依然是模板题,可以从前序遍历和后序遍历来考虑。
题解:
层序遍历:
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> que;
int depth{0};
if (root != nullptr) que.push(root);
while(!que.empty())
{
int size = que.size();
++depth;
for (int i = 0; i < size; ++i)
{
TreeNode* node = que.front();
que.pop();
if (!node->left && !node->right) return depth;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return depth;
}
};
前序遍历:
class Solution {
public:
int result{INT_MAX};
void getDepth(TreeNode* node, int depth)
{
if (node == nullptr) return;
if (node->left == nullptr && node->right == nullptr)
result = depth < result ? depth : result;
if (node -> left)
{
++depth;
getDepth(node->left, depth);
--depth;
}
if (node -> right)
{
++depth;
getDepth(node->right, depth);
--depth;
}
return;
}
int minDepth(TreeNode* root) {
if (root == nullptr) return 0;
getDepth(root, 1);
return result;
}
};
后序遍历:
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == nullptr)
return 0;
if (root->left == nullptr && root -> right == nullptr)
return 1;
if (root->left == nullptr)
return 1 + minDepth(root->right);
if (root -> right == nullptr)
return 1 + minDepth(root->left);
return 1 + min(minDepth(root->left), minDepth(root->right));
}
};