递归遍历翻转
/**
* 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) {
reverse(root);
return root;
}
void reverse(TreeNode* node){
if(node==NULL) return;
reverse(node->left);
reverse(node->right);
TreeNode *tmp=node->left;
node->left=node->right;
node->right=tmp;
}
};
层序遍历翻转
/**
* 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==NULL) return root;
queue<TreeNode*>q;
q.push(root);
while(!q.empty()){
TreeNode* node=q.front();
TreeNode* tmp;
q.pop();
tmp=node->left;
node->left=node->right;
node->right=tmp;
if(node->left!=NULL) q.push(node->left);
if(node->right!=NULL) q.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:
bool isSymmetric(TreeNode* root) {
return compare(root->left,root->right);
}
bool compare(TreeNode* l, TreeNode* r){
if(l==NULL&&r==NULL) return true;
else if(l==NULL||r==NULL) return false;
else if(l->val!=r->val) return false;
bool in,out;
in=compare(l->right,r->left);
out=compare(l->left,r->right);
return in&&out;
}
};
递归的本质上是在从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 isSymmetric(TreeNode* root) {
queue<TreeNode*>q;
q.push(root->left);
q.push(root->right);
while(!q.empty()){
TreeNode* l=q.front();
q.pop();
TreeNode* r=q.front();
q.pop();
if(r==l){
continue;
}else if(r==NULL||l==NULL){
return false;
}else if(r->val!=l->val){
return false;
}
q.push(l->left);
q.push(r->right);
q.push(l->right);
q.push(r->left);
}
return true;
}
};
同时对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) {
return read(root,0);
}
int read(TreeNode* node, int d){
if(node==NULL) return d;
int l=read(node->left,d+1);
int r=read(node->right,d+1);
if(l>r) return l;
return r;
}
};
不难,没什么好说的。
/**
* 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==NULL) return 0;
return read(root,0);
}
int read(TreeNode* node, int d){
int l=100000,r=100000;
if(node->left==NULL&&node->right==NULL) return d+1;
if(node->left!=NULL) l=read(node->left,d+1);
if(node->right!=NULL) r=read(node->right,d+1);
if(l<r) return l;
return r;
}
};
只要注意最小深度的求取需要碰到左右子节点都为NULL才能返回值即可。