LeetCode 110平衡二叉树 257二叉树所有的路径 304 左叶子之和 | 代码随想录25期训练营day17

LeetCode 110 平衡二叉树 2023.11.10

class Solution {
public:
    //后序递归遍历
    // int getheight(TreeNode* root)
    // {
    //     //终止条件
    //     if(root == NULL)
    //         return 0;
    //     int leftheight = getheight(root->left);//左
    //     //一般性条件,左子树高度==-1返回-1
    //     if(leftheight == -1)
    //         return -1;
    //     int rightheight = getheight(root->right);//右
    //     //一般性条件,右子树高度==-1返回-1
    //     if(rightheight == -1)
    //         return -1;
    //     //中
    //     return (abs(leftheight - rightheight) > 1 ? -1 : 1 + max(leftheight, rightheight));
    // }

    //前序迭代
    int getdepth(TreeNode* cur)
    {
        int depth = 0;
        int result = 0;
        if(cur == NULL)
            return result;
        stack<TreeNode*> st;
        st.push(cur);
        while (!st.empty())
        {
            TreeNode* node = st.top();
            st.pop();
            if(node != NULL)
            {
                st.push(node);
                st.push(NULL);
                depth++;
                if(node->left)
                    st.push(node->left);
                if(node->right)
                    st.push(node->right);
            }
            else
            {
                st.pop();
                depth--;
            }
            result = result > depth ? result : depth;
        }
        return result;
    }

    bool isBalanced(TreeNode* root) {
        //后序递归遍历
        // if(getheight(root) == -1)
        //     return false;
        // else
        //     return true;

        //前序迭代遍历
        if(root == NULL)
            return true;
        stack<TreeNode*> st;
        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->left)
                st.push(node->left);
            if(node->right)
                st.push(node->right);
        }
        return true;
    }
};

LeetCode 257 二叉树所有的路径 2023.11.10

    vector<string> binaryTreePaths(TreeNode* root) {
        //前序遍历
        // vector<int> path;
        // vector<string> result;
        // traversal(root, path, result);
        // return result;

        //迭代法
        stack<TreeNode*> treest;
        stack<string> pathst;
        vector<string> result;
        if(root == NULL)
            return result;
        treest.push(root);
        pathst.push(to_string(root->val));
        while (!treest.empty())
        {
            TreeNode* temp = treest.top();
            treest.pop();
            string path = pathst.top();
            pathst.pop();
            if(temp->left == NULL && temp->right == NULL)
                result.push_back(path);
            if(temp->left)
            {
                treest.push(temp->left);
                pathst.push(path + "->" + to_string(temp->left->val));
            }
            if(temp->right)
            {
                treest.push(temp->right);
                pathst.push(path + "->" + to_string(temp->right->val));
            }
        }
        return result;
    }
};

LeetCode 304 左叶子之和 2023.11.10

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        //递归遍历
        // int result = 0;
        // if(root == NULL)
        //     return 0;
        // if(root->left == NULL && root->right == NULL)
        //     return 0;
        // int leftleaves = sumOfLeftLeaves(root->left);
        // if(root->left && !root->left->left && !root->left->right)
        //     leftleaves = root->left->val;
        // int rightleaves = sumOfLeftLeaves(root->right);
        // int sum = leftleaves + rightleaves;
        // return sum;

        //迭代遍历
        int result = 0;
        if(root == NULL)
            return result;
        stack<TreeNode*> st;
        st.push(root);
        while (!st.empty())
        {
            TreeNode* node = st.top();
            st.pop();
            if(node->left && !node->left->left && !node->left->right)
                result += node->left->val;
            if(node->left)
                st.push(node->left);
            if(node->right)
                st.push(node->right);
        }
        return result;
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值