代码随想录算法训练营第十七天 | 110. 平衡二叉树 257. 二叉树的所有路径 404.左叶子之和

110.平衡二叉树

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        // 使用层序遍历(不行,有bug)
        queue<TreeNode*> que;
        if(root == NULL) return true;
        que.push(root);
        bool start = false;
        int result = 0;
        while(!que.empty()) {
            int size = que.size();
            for(int i = 0; i < size; i++) {
                
                TreeNode* tmp = que.front();printf("%d\n",tmp->val);
                que.pop();
                if(tmp->left) que.push(tmp->left);
                else start = true;
                if(tmp->right) que.push(tmp->right);
                else start = true;
            }
            printf("--%d\n",result);
            if(start == true) result++;
            if(result > 2) {
                printf("res = %d",result);
                return false;
            }
        }
        return true;
    }
};
class Solution {
public:
    int getHeight(TreeNode* root) {
        if(root == NULL) return 0;
        int left = getHeight(root->left);
        if(left == -1) return -1;
        int right = getHeight(root->right);
        if(right == -1) return -1;
        printf("%d %d %d\n", max(left,right),left,right);
        if(abs(left-right) > 1) {
            return -1;
        }
        return max(left,right) + 1;
    }
    bool isBalanced(TreeNode* root) {
        // 使用递归后序遍历
        if(getHeight(root) == -1) return false;
        return true;
    }
};

257.二叉树的所有路径

题目链接:力扣

class Solution {
public:
    
    void traversal(TreeNode* node,vector<int>& vec, vector<string>& result) {
        if(node == NULL) {
            return;
        }
​
        vec.push_back(node->val);
        if(node->left) {
            traversal(node->left,vec,result);
            vec.pop_back();
        }
        if(node->right) {
            traversal(node->right,vec,result);
            vec.pop_back();
        } 
        if(node->left == NULL && node->right == NULL) {
            string str = to_string(vec[0]);
            for(int i = 1; i < vec.size(); i++) {
                str += "->";
                str += to_string(vec[i]);
            }
            result.push_back(str);
            
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        // 递归前序遍历
        vector<int> vec;
        vector<string> result;
        traversal(root,vec,result);
        return result;
    }
};
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        // 迭代法
        vector<string> result; // 存放最后结果
        stack<string> path; // 存放当前节点路径
        if(root == NULL) return result;
​
        stack<TreeNode*> st;
        st.push(root);
        path.push(to_string(root->val));
​
        while(!st.empty()) {
            TreeNode* cur = st.top();
            st.pop();
            string str = path.top();
            path.pop();
            if(cur->left == NULL && cur->right == NULL) {
                result.push_back(str);
            }
            if(cur->right) {
                st.push(cur->right);
                path.push(str + "->" + to_string(cur->right->val));
            }
            if(cur->left) {
                st.push(cur->left);
                path.push(str + "->" + to_string(cur->left->val));
            }
            
        }
        return result;
    }
};

404.左叶子之和

题目链接:力扣

class Solution {
public:
    int traversal(TreeNode* node) {
        if(node == NULL) return 0;
        if(node->left == NULL && node->right == NULL) return 0;
        int leftNum = 0;
        if(node->left != NULL && node->left->left == NULL && node->left->right == NULL) {
            leftNum =  node->left->val;
        }
        return leftNum + traversal(node->left) + traversal(node->right);
    }
    int sumOfLeftLeaves(TreeNode* root) {
        // 递归前序遍历
        return traversal(root);
    }
};
class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        // 迭代法,前序
        int result = 0;
        if(root == NULL) return result;
        stack<TreeNode*> st;
        st.push(root);
        while(!st.empty()) {
            TreeNode* cur = st.top();
            st.pop();
​
            if(cur->left && cur->left->left == NULL && cur->left->right == NULL) {
                result += cur->left->val;
            }
            if(cur->right) {
                st.push(cur->right);
            }
            if(cur->left) {
                st.push(cur->left);
            }
        }
        return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值