day 6 二叉树

 递归三部曲:

1.确定递归函数的参数和返回值

2.确定终止条件

3.确定单层递归的逻辑

144. 二叉树的前序遍历


 

递归:

/**
 * 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:
    void traversal(TreeNode* cur,vector<int>& res){
        if(cur==nullptr) return;
        res.push_back(cur->val);
        traversal(cur->left,res);
        traversal(cur->right,res);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        traversal(root,res);
        return res;
    }
};

 迭代法:

/**
 * 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:
    vector<int> preorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> res;
        if(root==nullptr) return res;
        st.push(root);
        while(!st.empty()){
            TreeNode* cur=st.top();
            res.push_back(cur->val);
            st.pop();
            if(cur->right) st.push(cur->right);
            if(cur->left) st.push(cur->left);
        }
        return res;
    }
};

145. 二叉树的后序遍历

递归法:

/**
 * 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:
    void traversal(TreeNode* cur,vector<int>& res){
        if(cur==nullptr) return;
        traversal(cur->left,res);
        traversal(cur->right,res);
        res.push_back(cur->val);
    }
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        traversal(root,res);
        return res;
    }
};

迭代法:

/**
 * 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:
    vector<int> postorderTraversal(TreeNode* root) {
        stack<TreeNode*> st;
        vector<int> res;
        if(root==nullptr) return res;
        st.push(root);
        while(!st.empty()){
            TreeNode* cur=st.top();
            res.push_back(cur->val);
            st.pop();
            if(cur->left) st.push(cur->left);
            if(cur->right) st.push(cur->right);
        }
        reverse(res.begin(),res.end());
        return res;
    }
};

94. 二叉树的中序遍历

递归法:

/**
 * 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:
        void traversal(TreeNode* cur,vector<int>& res){
        if(cur==nullptr) return;
        traversal(cur->left,res);
        res.push_back(cur->val);
        traversal(cur->right,res);
        
    }
    vector<int> inorderTraversal(TreeNode* root) {
         vector<int> res;
        traversal(root,res);
        return res;
    }
};

迭代法:

/**
 * 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:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int>res;
        stack<TreeNode*> st;
        TreeNode* cur=root;
        while(cur!=nullptr||!st.empty()){
            if(cur!=nullptr){
                st.push(cur);
                cur=cur->left;
            }
            else{
                cur=st.top();
                st.pop();
                res.push_back(cur->val);
                cur=cur->right;
            }
        }
        return res;
    }
};

102. 二叉树的层序遍历

/**
 * 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:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> que;
        vector<vector<int>> res;
        if(root==nullptr) return res;
        TreeNode* cur=root;
        que.push(cur);
        while(!que.empty()){
            int size=que.size();//确定每层的节点数量
            vector<int> vec;
            for(int i=0;i<size;i++){//根据上层的节点数量处理上层
                cur=que.front();
                vec.push_back(cur->val);//将上层节点值压入res,弹出que
                que.pop();
                if(cur->left) que.push(cur->left);//将弹出que的节点的左右孩子节点压入que;
                if(cur->right) que.push(cur->right);
            }
            res.push_back(vec);
        }
        return res;
    }
};

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) {
        queue<TreeNode*> que;
        if(root!=nullptr) que.push(root);
        while(!que.empty()){
            int size=que.size();
            for(int i=0;i<size;i++){
                TreeNode* cur=que.front();
                que.pop();
                swap(cur->left,cur->right);
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->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 compare(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;//左右子树

        //单层循环
        bool outside=compare(left->left,right->right);
        bool inside=compare(left->right,right->left);
        bool issame=outside&&inside;
        return issame;
    }
    bool isSymmetric(TreeNode* root) {
        if(root==nullptr) return true;
        return compare(root->left,root->right);
    }
};

104. 二叉树的最大深度

后序:

/**
 * 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==nullptr) return 0;

    //确定单层循环逻辑
    int leftDept=maxDepth(root->left);
    int rightDept=maxDepth(root->right);
    int dept=max(leftDept,rightDept);
    return dept+1;
    }
};

前序:

/**
 * 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 res;
    void getDept(TreeNode* root,int depth){//确定递归参数和返回值
        //确定终止条件
        if(root==nullptr) return;

        //确定单层循环
        res=depth>res?depth:res;

        //左孩子
        depth++;
        getDept(root->left,depth);
        depth--;
        //右孩子
        depth++;
        getDept(root->right,depth);
        depth--;

        return;
    }
    int maxDepth(TreeNode* root) {
        res=1;
        if(root==nullptr) return 0;
        getDept(root,res);
        return res;
    }
};

层序:

/**
 * 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==nullptr) return 0;
        int depth=0;
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()){
            int size=que.size();
            depth++;
            for(int i=0;i<size;i++){
                TreeNode* cur=que.front();
                que.pop();
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
        }
        return depth;
    }
};

111. 二叉树的最小深度

判断收获的地方的时候,是这个节点的左右子节点都为空的时候,并且注意是叶节点到根节点。如果节点数是1,那深度就是1,但是如果节点不是1,就要注意最短一定不是1。

后序:

/**
 * 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==nullptr) return 0;

    //确定单层递归逻辑
    int leftDepth=minDepth(root->left);

    int rightDepth=minDepth(root->right);

    int depth;
    if(root->left&&!root->right) depth=1+leftDepth;
    else if(!root->left&&root->right) depth=1+rightDepth;
    else depth=min(leftDepth,rightDepth)+1;

    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 res;
    void getDepth(TreeNode* cur,int depth){//确定函数参数和返回值
        //确定终止条件
        if(cur==nullptr) return;

        //确定单层递归逻辑
        if(cur->left==nullptr&&cur->right==nullptr){
            res=min(depth,res);
        }
        getDepth(cur->left,depth+1);
        getDepth(cur->right,depth+1);
        return;
    } 
    int minDepth(TreeNode* root) {
        if(root==nullptr) return 0;
        res=INT_MAX;
        getDepth(root,1);
        return res;
    }
};

迭代法:(层序)

/**
 * 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;
        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* cur=que.front();
                que.pop();
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
                if(cur->left==nullptr&&cur->right==nullptr) return depth;
            }
        }
        return depth;

    }
};

222. 完全二叉树的节点个数

迭代法:层序遍历

/**
 * 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 countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if(root!=nullptr) que.push(root);
        int res=0;
        while(!que.empty()){
            int size=que.size();
            res+=size;
            for(int i=0;i<size;i++){
                TreeNode* cur=que.front();
                que.pop();
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
        }
        return res;
    }
};

递归法:(后序遍历)

/**
 * 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 getNum(TreeNode* cur){//确定递归函数参数和返回值
        //确定终止条件
        if(cur==nullptr) return 0;

        //确定单层递归逻辑
        int leftNum=getNum(cur->left);
        int rightNum=getNum(cur->right);
        int midNum=leftNum+rightNum+1;
        return midNum;
    }
    int countNodes(TreeNode* root) {
        return getNum(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 geNum(TreeNode* cur){//确定递归函数参数和返回值
        if(cur==nullptr) return 0;
        //确定终止条件
        int leftNum=0,rightNum=0;
        TreeNode* left=cur->left;
        TreeNode* right=cur->right;
        while(left){
            leftNum++;
            left=left->left;
        }
        while(right){
            rightNum++;
            right=right->right;
        }
        if(leftNum==rightNum) return (2<<leftNum)-1;

        //单层递归逻辑
        int leftTreeNum=geNum(cur->left);
        int rightTreeNum=geNum(cur->right);
        int midTreeNum=leftTreeNum+rightTreeNum+1;

        return midTreeNum;
    }
    int countNodes(TreeNode* root) {
        return geNum(root);
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值