leetcode 226翻转二叉树
递归
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL) return root;
TreeNode* node =root;
swap(node->left,node->right);
invertTree(node->left);
invertTree(node->right);
return node;
}
};
前序
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL) return root;
stack<TreeNode*>st;
TreeNode* node =root;
st.push(node);
while(!st.empty()){
node=st.top();
if(node!=NULL){
st.pop();
if(node->left) st.push(node->left);
if(node->right) st.push(node->right);
st.push(node);
st.push(NULL);
}else{
st.pop();
node=st.top();
st.pop();
swap(node->left,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==NULL) return root;
queue<TreeNode*>q;
TreeNode* node =root;
q.push(root);
while(!q.empty()){
int size=q.size();
for(int i=0;i<size;i++){
node=q.front();
q.pop();
if(node->left)q.push(node->left);
if(node->right)q.push(node->right);
swap(node->left,node->right);
}
}
return root;
}
};
LeetCode 101.对称二叉树
递归
class Solution {
public:
bool isCompare(TreeNode* l,TreeNode* r){
if(l==NULL&&r==NULL)return true;
if(l==NULL&&r!=NULL)return false;
if(l!=NULL&&r==NULL)return false;
if(l->val!=r->val) return false;
bool lb =isCompare(l->left,r->right);//注意是轴对称所以要比较左的左,右的右
bool rb=isCompare(l->right,r->left);//比较右的左,左的右
return lb&&rb;
}
bool isSymmetric(TreeNode* root) {
if(root==NULL) return true;
return isCompare(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==NULL) return true;
TreeNode* l=root->left;
TreeNode* r=root->right;
stack<TreeNode*> st;
st.push(l);
st.push(r);
while(!st.empty()){
l=st.top();
st.pop();
r=st.top();
st.pop();
if(!l&&!r){ // 左节点为空、右节点为空,此时说明是对称的
continue;
}
if(!l||!r||(l->val!=r->val)){
return false;
}
st.push(l->right);
st.push(r->left);
st.push(l->left);
st.push(r->right);
}
return true;
}
};
二叉树最大深度
递归
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==NULL){
return 0;
}
int l=0,r=0;
if(root->left){
l=maxDepth(root->left);
}
if(root->right){
r=maxDepth(root->right);
}
int max1=max(l,r);
return 1+max1;
}
};
层序
层序遍历的层数不就是最大深度吗
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==NULL){
return 0;
}
queue<TreeNode*>q;
TreeNode* node;
q.push(root);
int dep=0;
while(!q.empty()){
int size=q.size();
dep++;
for(int i=0;i<size;i++){
node=q.front();
q.pop();
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
}
return dep;
}
};
补一下前序版
class Solution {
public:
int result;
void getDepth(TreeNode* node, int depth) {
result = depth > result ? depth : result; // 中
if (node->left == NULL && node->right == NULL) return ;
if (node->left) { // 左
getDepth(node->left, depth + 1);
}
if (node->right) { // 右
getDepth(node->right, depth + 1);
}
return ;
}
int maxDepth(TreeNode* root) {
result = 0;
if (root == 0) return result;
getDepth(root, 1);
return result;
}
};
求最小深度,那就在层序遍历的过程中截断就好了
/**
* 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;
}
queue<TreeNode*>q;
TreeNode* node;
q.push(root);
int dep=0;
while(!q.empty()){
int size=q.size();
dep++;
for(int i=0;i<size;i++){
node=q.front();
q.pop();
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
if(node->left==NULL&&node->right==NULL){
break;
}
}
if(node->left==NULL&&node->right==NULL){
break;
}
}
return dep;
}
};
递归
class Solution {
public:
int getDepth(TreeNode* node) {
if (node == NULL) return 0;
int leftDepth = getDepth(node->left); // 左
int rightDepth = getDepth(node->right); // 右
// 中
// 当一个左子树为空,右不为空,这时并不是最低点
if (node->left == NULL && node->right != NULL) {
return 1 + rightDepth;
}
// 当一个右子树为空,左不为空,这时并不是最低点
if (node->left != NULL && node->right == NULL) {
return 1 + leftDepth;
}
int result = 1 + min(leftDepth, rightDepth);
return result;
}
int minDepth(TreeNode* root) {
return getDepth(root);
}
};