DAY17|110.平衡二叉树 ● 257. 二叉树的所有路、404.左叶子之和

文章提供了几个关于二叉树的解决方案:检查二叉树是否平衡,找到所有从根节点到叶节点的路径,以及计算所有左叶子节点的和。使用深度优先搜索策略进行递归遍历,对于平衡二叉树,比较左右子树的高度差;对于路径查找,将路径节点值拼接成字符串;对于左叶子之和,递归地访问每个节点并累加符合条件的左叶子节点的值。
摘要由CSDN通过智能技术生成

110.平衡二叉树 

class Solution {
public:
    // 返回以该节点为根节点的二叉树的高度,如果不是平衡二叉树了则返回-1
    int getHeight(TreeNode* node) {
        if (node == NULL) {
            return 0;
        }
        int leftHeight = getHeight(node->left);
        if (leftHeight == -1) return -1;
        int rightHeight = getHeight(node->right);
        if (rightHeight == -1) return -1;
        return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
    }
    bool isBalanced(TreeNode* root) {
        return getHeight(root) == -1 ? false : true;
    }
};
class Solution {
private:
    int getDepth(TreeNode* cur) {
        stack<TreeNode*> st;
        if (cur != NULL) st.push(cur);
        int depth = 0; // 记录深度
        int result = 0;
        while (!st.empty()) {
            TreeNode* node = st.top();
            if (node != NULL) {
                st.pop();
                st.push(node);                          // 中
                st.push(NULL);
                depth++;
                if (node->right) st.push(node->right);  // 右
                if (node->left) st.push(node->left);    // 左

            } else {
                st.pop();
                node = st.top();
                st.pop();
                depth--;
            }
            result = result > depth ? result : depth;
        }
        return result;
    }

public:
    bool isBalanced(TreeNode* root) {
        stack<TreeNode*> st;
        if (root == NULL) return true;
        st.push(root);
        while (!st.empty()) {
            TreeNode* node = st.top();                       // 中
            st.pop();
            if (abs(getDepth(node->left) - getDepth(node->right)) > 1) {
                return false;
            }
            if (node->right) st.push(node->right);           // 右(空节点不入栈)
            if (node->left) st.push(node->left);             // 左(空节点不入栈)
        }
        return true;
    }
};

  257. 二叉树的所有路

class Solution {
public:
    vector<string> res;
    vector<int> path;
    vector<string> binaryTreePaths(TreeNode* root) 
    {
        if(root==nullptr)
        return res;
        traversal(root,path,res);
        return res;
    }
    void traversal(TreeNode*cur,vector<int>&path,vector<string>&res)
    {
        path.push_back(cur->val);
        if(cur->left==nullptr&&cur->right==nullptr)
        {
            string Path;
            for(int i=0;i<path.size()-1;i++)
            {
                Path+=to_string(path[i]);
                Path+="->";

            }
            Path+=to_string(path[path.size()-1]);
            res.push_back(Path);
            return ;
        }
        if(cur->left)
        {
            traversal(cur->left,path,res);
            path.pop_back();
        }
         if(cur->right)
        {
            traversal(cur->right,path,res);
            path.pop_back();
        }
    }

    //     dfs(root,path);
    //     return res;

    // }
    // void  dfs(TreeNode*root,string path)
    // {
    //     if(root==nullptr)
    //     return;
    //     path+=to_string(root->val);
     
    //     if(!root->left&&!root->right)
    //     {
    //         res.push_back(path);
    //          return;
    //     }
       
    //     dfs(root->right,path+"->");
    //     dfs(root->left,path+"->");
    
};


//版本二
class Solution {
private:
    void traversal(TreeNode* cur, string path, vector<string>& result) {
        path += to_string(cur->val); // 中,中为什么写在这里,因为最后一个节点也要加入到path中
        if (cur->left == NULL && cur->right == NULL) {
            result.push_back(path);
            return;
        }
        if (cur->left) {
            path += "->";
            traversal(cur->left, path, result); // 左
            path.pop_back(); // 回溯 '>'
            path.pop_back(); // 回溯 '-'
        }
        if (cur->right) {
            path += "->";
            traversal(cur->right, path, result); // 右
            path.pop_back(); // 回溯'>'
            path.pop_back(); // 回溯 '-'
        }
    }

public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        string path;
        if (root == NULL) return result;
        traversal(root, path, result);
        return result;

    }
};

404.左叶子之和

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) 
    {


    
   
        if(root==nullptr)
        return 0;
        if(root->left==nullptr&&root->right==nullptr)
        return 0;
        int leftvalue=sumOfLeftLeaves(root->left);
        if(root->left&&!root->left->left&&!root->left->right)
        {
            leftvalue+=root->left->val;
        }
        int rightvalue=sumOfLeftLeaves(root->right);
        int sum=leftvalue+rightvalue;
        return sum;

    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值