二叉树层序遍历与翻转、对称

二叉树层序遍历

102.二叉树的层序遍历
用队列做一遍:
做的过程中对于queue队列的运用还不是很熟,

push() 在队尾插入一个元素
pop() 删除队列第一个元素
size() 返回队列中元素个数
empty() 如果队列空则返回true
front() 返回队列中的第一个元素
back() 返回队列中最后一个元素

如上,queue没有top,push要对应front()来用。

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> res;
        if(root) res.push(root);
        vector<vector<int>> result;
        while(!res.empty()) {
            int size=res.size();
            vector<int> q;
            for(int i=0;i<size;i++) {
                TreeNode* p=res.front();
                q.push_back(p->val);
                res.pop();
                if(p->left) res.push(p->left);
                if(p->right) res.push(p->right);
            }
            result.push_back(q);
        }
        return result;
    }
};

递归解法:

class Solution {
public:
    void order(TreeNode* root,int depth,vector<vector<int>>& result) {
        if(root==nullptr) return;
        if(depth==result.size()) result.push_back(vector<int>());
        result[depth].push_back(root->val);
        order(root->left,depth+1,result);
        order(root->right,depth+1,result);
    }
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> result;
        order(root,0,result);
        return result;
    }
};

107.二叉树的层序遍历

class Solution {
public:
    void order(TreeNode* root,int depth,vector<vector<int>>& result) {
        if(root==nullptr) return;
        if(depth==result.size()) result.push_back(vector<int>());
        result[depth].push_back(root->val);
        order(root->left,depth+1,result);
        order(root->right,depth+1,result);
    }
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> result;
        order(root,0,result);
        reverse(result.begin(),result.end());
        return result;
    }
};

199.二叉树的右视图

class Solution {
public:
    void CenXu(TreeNode* root,int depth,vector<vector<int>>& result){
        if(root==nullptr) return;
        if(depth==result.size()) result.push_back(vector<int>());
        result[depth].push_back(root->val);
        CenXu(root->left,depth+1,result);
        CenXu(root->right,depth+1,result); 
    }
    vector<int> rightSideView(TreeNode* root) {
        vector<vector<int>> result;
        vector<int> res;
        CenXu(root,0,result);
        for(int i=0;i<result.size();i++) {
            res.emplace_back(result[i].back());
        }
        return res;
    }
};

637.二叉树的层平均值

class Solution {
public:
    void Order(TreeNode* root,int depth,vector<vector<int>>& result) {
        if(root==nullptr) return;
        if(depth==result.size()) result.push_back(vector<int>());
        result[depth].push_back(root->val);
        Order(root->left,depth+1,result);
        Order(root->right,depth+1,result);
    }
    vector<double> averageOfLevels(TreeNode* root) {
        vector<vector<int>> result;
        Order(root,0,result);
        vector<double> res;
        for(int i=0;i<result.size();i++) {
            double sum=0;double t=result[i].size();
            for(int j=0;j<result[i].size();j++) {
                sum+=result[i][j];
            }
            res.emplace_back(sum/t);
        }
        return res;
    }
};

429.n-叉树层序遍历

class Solution {
public:
    void Order(Node* root,int depth,vector<vector<int>>& result){
        if(root==nullptr) return ;
        if(depth==result.size()) result.push_back(vector<int>());
        result[depth].push_back(root->val);
        for(int i=0;i<root->children.size();i++) {
            Order(root->children[i],depth+1,result);
        }

    }
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>> result;
        Order(root,0,result);
        return result;
    }
};

515.在每个树行中找最大值

class Solution {
public:
    void Order(TreeNode* root,int depth,vector<vector<int>>&result) {
        if(root==nullptr) return ;
        if(depth==result.size()) result.push_back(vector<int>());
        result[depth].push_back(root->val);
        Order(root->left,depth+1,result);
        Order(root->right,depth+1,result);
    }
    vector<int> largestValues(TreeNode* root) {
        vector<vector<int>> result;
        Order(root,0,result);
        vector<int> res;
        for(int i=0;i<result.size();i++) {
            int max_=result[i][0];
            for(int j=1;j<result.size();j++) {
                
            }
        }
    }
};

116.填充每个节点的下一个右侧节点指针


class Solution {
public:
    void Order(Node* root,int depth,vector<vector<Node*>>&result) {
        if(root==nullptr) return;
        if(depth==result.size()) result.push_back(vector<Node*>());
        result[depth].push_back(root);
        Order(root->left,depth+1,result);
        Order(root->right,depth+1,result);
    }
    Node* connect(Node* root) {
        vector<vector<Node*>> result;
        Order(root,0,result);
        for(int i=0;i<result.size();i++) {
            for(int j=1;j<result[i].size();j++) {
                result[i][j-1]->next=result[i][j];
            }
            Node* t=result[i].back();
            t->next=nullptr;
        } 
        
        return root;
    }
};

104.二叉树的深度

class Solution {
public:
    void Order(TreeNode* root,int depth,int &maxDepth)  {
        if(root==nullptr) return ;
        maxDepth=max(maxDepth,depth);
        Order(root->left,depth+1,maxDepth);
        Order(root->right,depth+1,maxDepth);
    }
    int maxDepth(TreeNode* root) {
        int depth=0;
        Order(root,1,depth);
        return depth;
    }
};

11.二叉树的最小深度

class Solution {
public:
    void Order(TreeNode* root,int depth,int& minDepth) {
        if(root->left==nullptr&&root->right==nullptr) {
            minDepth=min(minDepth,depth);
        }else{
            if(root->left!=nullptr) {
                Order(root->left,depth+1,minDepth);
            }
            if(root->right!=nullptr) {
                Order(root->right,depth+1,minDepth);
            }
        }
    }
    int minDepth(TreeNode* root) {
        int minD=INT_MAX;
        if(root!=nullptr)
            Order(root,1,minD);
        else {
            minD=0;
        }
        return minD;
    }
};

翻转二叉树

226.翻转二叉树
递归写法,把左右节点交换,并且递归左节点、右节点使得左节点、右节点做相同的操作

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root==nullptr) return nullptr;
        TreeNode* left=root->left;
        TreeNode* right=root->right;
        root->left=right;
        root->right=left;
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

非递归写法,对于广度遍历使用队列,不断入队左右节点,当然也是之前需要先交换左右节点;

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root==nullptr) return nullptr;
        queue<TreeNode*> st;
        st.push(root);
        while(!st.empty()) {
            TreeNode* node=st.front();
            st.pop();
            swap(node->left,node->right);
            if(node->left) st.push(node->left);
            if(node->right) st.push(node->right);
        }
        return root;
    }
};

试着使用统一风格的前序遍历(非递归遍历)来写的翻转写法,跟之前差不多写法,就是需要左右节点互换一下;

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root==nullptr) return root;
        stack<TreeNode*> st;
        st.push(root);

        while(!st.empty()) {

            TreeNode* node=st.top();
            if(node) {
                st.pop();
                swap(node->left,node->right);

                if(node->right) st.push(node->right);
                if(node->left) st.push(node->left);
                st.push(node);
                st.push(nullptr);
            }else{
                st.pop();
                st.pop();
            }
        }
        return root;
    }
};

101.对称二叉树
递归解法,不断比较左右子树,要比较的话,就是得不断比较左子树和右子树:

class Solution {
public:

    bool compare(TreeNode* left,TreeNode* right) {

        if(left==nullptr&&right==nullptr) return true;
        else if(left==nullptr&&right!=nullptr) return false;
        else if(left!=nullptr&&right==nullptr) return false;
        else if(left!=nullptr&&right!=nullptr&&left->val!=right->val) return false;
        bool Isleft=compare(left->left,right->right);
        bool Isright=compare(left->right,right->left);

        return Isleft&&Isright;

    }
    bool isSymmetric(TreeNode* root) {
        if(root==nullptr) return true;
        return compare(root->left,root->right);

    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值