day 7 二叉树H`1

前序遍历求树的深度,后序遍历求树的高度。深度是从根节点出发计算,高度是从叶子结点出发开始计算。

110. 平衡二叉树

后序:

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

        //确定单层递归逻辑
        int leftNum=getHeight(cur->left);
        int rightNum=getHeight(cur->right);
        if(leftNum==-1) return -1;
        if(rightNum==-1) return -1;
        if(abs(leftNum-rightNum)>1) return -1;
        else return max(leftNum,rightNum)+1;
    }
    bool isBalanced(TreeNode* root) {
        int res=getHeight(root);
        if(res==-1) return false;
        else return true;
    }
};

257. 二叉树的所有路径

与求深度相似,用前序

/**
 * 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 travel(TreeNode* cur,vector<int>& path,vector<string>&res){
        path.push_back(cur->val);//应该在递归结束前把叶子结点也加入path
       //确定终止条件
       if(cur->left==nullptr&&cur->right==nullptr){
        string spath="";
        for(int i=0;i<path.size()-1;i++){
            spath+=to_string(path[i]);
            spath+="->";
        }
        spath+=to_string(path[path.size()-1]);
        res.push_back(spath);
       }

       //确定单层递归逻辑
       
       if(cur->left){
        travel(cur->left,path,res);
        path.pop_back();//pop的是cur->left->val
       }
       if(cur->right){
        travel(cur->right,path,res);
        path.pop_back();
       }
        return;

    }
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<int>path;
        vector<string>res;
        travel(root,path,res);
        return res;
    }
};

404. 左叶子之和

自己写的:(其实暗含的有终止条件,因为我都加了if条件判断了,只要不满足就不会执行if后的语句,而且主要函数是void类型,走到函数整体结尾就结束了。)

/**
 * 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 traversal(TreeNode* cur){//确定递归函数参数和返回值
        //确定终止条件
       

        //确定单层递归逻辑
        if(cur->left!=nullptr&&cur->left->left==nullptr&&cur->left->right==nullptr){
            res+=cur->left->val;
        }
        if(cur->left){
            traversal(cur->left);
        }
        if(cur->right){
            traversal(cur->right);
        }
    }
    int sumOfLeftLeaves(TreeNode* root) {
        if(root==nullptr) return 0;
        res=0;
        traversal(root);
        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 sumOfLeftLeaves(TreeNode* root) {//确定递归函数参数和返回值
    //确定终止条件
    if(root==nullptr) return 0;//节点为空 -1层
    if(root->left==nullptr&&root->right==nullptr) return 0;
    //节点的左右孩子为空 最后一层,叶子节点

    //确定单层递归逻辑
    int leftNum=sumOfLeftLeaves(root->left);
    if(root->left&&!root->left->left&&!root->left->right){
        leftNum=root->left->val;
    }
    int rightNum=sumOfLeftLeaves(root->right);
    int sum=leftNum+rightNum;
    return sum;
    }
};

513. 找树左下角的值

层序遍历:

/**
 * 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 findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> que;
        int res=0;
        if(root==nullptr) return 0;
        que.push(root);
        while(!que.empty()){
            int size=que.size();
            for(int i=0;i<size;i++){
                TreeNode* cur=que.front();
                que.pop();
                if(i==0) res=cur->val;
                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 res;
    int maxDepth=INT_MIN;
    void traversal(TreeNode* cur,int depth){//确定递归函数参数和返回值
        //确定终止条件
        if(cur->left==nullptr&&cur->right==nullptr){
            if(depth>maxDepth){
                res=cur->val;
                maxDepth=depth;
            } 
            return;
        }
        
        //确定单层递归逻辑
        if(cur->left){
            traversal(cur->left,depth+1);
        }
        if(cur->right){
            traversal(cur->right,depth+1);
        }
        return;
    }
    int findBottomLeftValue(TreeNode* root) {
        traversal(root,0);
        return res;
    }
};

112. 路径总和

递归法:

/**
 * 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 traversal(TreeNode* root,int sum){//确定递归函数参数和返回值
         //确定终止条件
        if(!root->left&&!root->right&&sum==0) return true;
        if(!root->left&&!root->right&&sum!=0) return false;

        //确定单层递归逻辑
        if(root->left){
            if(traversal(root->left,sum-root->left->val)) return true;
        }
        if(root->right){
            if(traversal(root->right,sum-root->right->val)) return true;
        }
        return false;
    }
    
    bool hasPathSum(TreeNode* root, int targetSum) {
       if(root==nullptr) return false;
       return traversal(root,targetSum-root->val);
    }
};

迭代法:

/**
 * 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 hasPathSum(TreeNode* root, int targetSum) {
        if(root==nullptr) return false;
        stack<pair<TreeNode*,int>> st;
        st.push(pair<TreeNode*,int>(root,root->val));
        while(!st.empty()){
            pair<TreeNode*,int> cur=st.top();
            st.pop();
            if(!cur.first->left&&!cur.first->right&&cur.second==targetSum) return true;
            if(cur.first->left){
                st.push(pair<TreeNode*,int>(cur.first->left,cur.second+cur.first->left->val));
            }
             if(cur.first->right){
                st.push(pair<TreeNode*,int>(cur.first->right,cur.second+cur.first->right->val));
            }
        }
        return false;
    }
};

106. 从中序与后序遍历序列构造二叉树

/**
 * 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* traversal(vector<int>& inorder,vector<int>& postorder){
        //确定终止条件
        if(postorder.size()==0) return nullptr;

        //单层递归逻辑
        //后序数组的最后一个值为中间值
        int rootvalue=postorder[postorder.size()-1];
        TreeNode* root=new TreeNode(rootvalue);

        //叶子结点
        if(postorder.size()==1) return root;

        //第三步:找切割点
        int divideIndex;
        for(int i=0;i<inorder.size();i++){
            if(inorder[i]==rootvalue) divideIndex=i;
        }

        //第四步:切割中序数组  左闭右开
        vector<int> leftInorder(inorder.begin(),inorder.begin()+divideIndex);
        vector<int> rightInorder(inorder.begin()+divideIndex+1,inorder.end());

        //第五步: 切割后序数组
        postorder.resize(postorder.size()-1);
        vector<int> leftPostoder(postorder.begin(),postorder.begin()+leftInorder.size());
        vector<int> rightPostorder(postorder.begin()+leftInorder.size(),postorder.end());
        //第六步: 递归
        root->left=traversal(leftInorder,leftPostoder);
        root->right=traversal(rightInorder,rightPostorder);
        return root;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        return traversal(inorder,postorder);
    }
};

654. 最大二叉树

/**
 * 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* constructMaximumBinaryTree(vector<int>& nums) {
        //确定终止条件
        TreeNode* node=new TreeNode(0);
        if(nums.size()==1){
            node->val=nums[0];
            return node;
        }

        //确定单层递归逻辑
        int maxValue=0;
        int maxIndex;
        for(int i=0;i<nums.size();i++){
            if(nums[i]>maxValue) {
                maxValue=nums[i];
                maxIndex=i;
            }
        }
        node->val=maxValue;
        //构造左子树
        if(maxIndex>0){
            vector<int> leftNums(nums.begin(),nums.begin()+maxIndex);
            node->left=constructMaximumBinaryTree(leftNums);
        }
        //构造右子树
        if(maxIndex<nums.size()-1){
            vector<int> rightNums(nums.begin()+maxIndex+1,nums.end());
            node->right=constructMaximumBinaryTree(rightNums);
        }
        return node;

    }
};

改进:

/**
 * 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* traversal(vector<int>& nums,int left,int right){
        //确定终止条件
        if(left==right) return nullptr;
        //确定单层递归逻辑
        int maxIndex=left;
        for(int i=left+1;i<right;i++){
            if(nums[i]>nums[maxIndex]) maxIndex=i;
        }
        TreeNode* node=new TreeNode(nums[maxIndex]);

        node->left=traversal(nums,left,maxIndex);
        node->right=traversal(nums,maxIndex+1,right);
        return node;
    }
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        return traversal(nums,0,nums.size());
        
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值