226.翻转二叉树
这里采用了前序遍历(后序遍历也可),本质是先处理节点,再不断访问左节点,到头(没有左子节点),回退到上一个节点再访问右节点,然后继续不断访问左节点......
/**
* 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;
swap(root->left, root->right);
if(root->left) invertTree(root->left);
if(root->right) invertTree(root->right);
return root;
}
};
101. 对称二叉树
对称的本质是左节点的左节点= 右节点的右节点,左节点的右节点 = 右节点的左节点。前中后遍历都只能访问一个节点的左右子节点,但这里必须同时知道左右节点的子节点的值,所以要同时访问两个树。话说这题算简单题吗。。。思路也蛮难想到的。
/**
* 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* left, TreeNode* right) {
if (left == NULL && right != NULL) return false;
else if (left != NULL && right == NULL) return false;
else if (left == NULL && right == NULL) return true;
else if (left->val != right->val) return false;
if(left->val != right->val) return false;
bool outer = isSame(left->left, right->right);
bool inner = isSame(left->right, right->left);
return outer && inner;
}
bool isSymmetric(TreeNode* root) {
if(!root) return false;
return isSame(root->left,root->right);
}
};
看到这题其实我第一反应是层序遍历,然后判断每层的元素是否对称即可。相比递归感觉这样思路更加清晰一些。要注意的点:不能忽略子节点是空结点的情况。
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (!root) 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) return false;
if (leftNode->val != rightNode->val) return false;
que.push(leftNode->left);
que.push(rightNode->right);
que.push(leftNode->right);
que.push(rightNode->left);
}
return true;
}
};
104.二叉树的最大深度
昨天用层序遍历写过一遍:while表示纵向的遍历(即不同层的遍历),for表示横向遍历(即同一层每一个节点的遍历)。所以把depth加入while循环中,求出答案即可。
/**
* 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:
int maxDepth(TreeNode* root) {
queue<TreeNode*> que;
if(root) que.push(root);
int depth = 0;
while(!que.empty()) {
int size = que.size();
depth++;
for(int i = 0; i < size; ++i) {
auto node = que.front();
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return depth;
}
};
再用递归做一遍,思路是:使用后序遍历,然后从最底下往上递推深度(最大深度即为回到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:
int maxDepth(TreeNode* root) {
if(!root) return 0;
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return 1 + max(leftDepth,rightDepth);
}
};
111.二叉树的最小深度
昨天也用了层序遍历解决的,思路是找到同一层中没有左右子节点的节点,即该路径已经到头了,顺势求出的深度为最小深度。
/**
* 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:
int minDepth(TreeNode* root) {
queue<TreeNode*> que;
if(root) que.push(root);
int depth = 0;
while(!que.empty()) {
int size = que.size();
depth++;
for(int i = 0; i < size; ++i) {
auto node = que.front();
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
if(!node->left && !node->right) return depth;
}
}
return depth;
}
};
· 下面用递归做一遍,这里要注意:和最大深度不同的是要单独处理该节点分别有左右节点的情况。比如一个节点有左节点而没有右节点,如果没有判断他就会计算得出最小深度位于其右节点的情况(但是右节点为空节点,不能作为最小深度计算)。
/**
* 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:
int minDepth(TreeNode* root) {
if(!root) return 0;
int leftDepth = minDepth(root->left);
int rightDepth = minDepth(root->right);
//下面这两个判断很关键
if(!root->left && root->right) return 1 + rightDepth;
if(!root->right && root->left) return 1 + leftDepth;
return 1 + min(leftDepth,rightDepth);
}
};
总结:求深度高度用前后序遍历,求节点顺序相关的题目(对称翻转)用中序遍历。递归很难,思路很隐晦,题目必须多刷。