代码随想录第14天|leetcode226.翻转二叉树、leetcode101.对称二叉树、leetcode.104.二叉树的最大深度、leetcode111.二叉树的最小深度

1.226. 翻转二叉树 - 力扣(LeetCode)

思路1:本题使用迭代法做较为直观,即建立一个队列。每个节点遍历之后,交换其左右孩子节点,随后将其左右孩子节点入栈。(这个不用遍历每一层,是因为这个不像是层次遍历,这个任何一个节点的左右孩子都要交换)

    TreeNode* invertTree(TreeNode* root) {
        if(root == nullptr)
            return nullptr;
        stack<TreeNode*> st;
        st.push(root);
        while(!st.empty()){
            TreeNode *node = st.top();
            st.pop();
            swap(node->left, node->right);
            if(node->left)
                st.push(node->left);
            if(node->right)
                st.push(node->right);
        }
        return root;
    }

思路二:广度优先遍历

层次遍历,也就是按照每一层的节点个数去交换他们的孩子。上一次是只要是节点就交换他们的孩子节点。

 TreeNode* invertTree(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                swap(node->left, node->right); // 节点处理
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return root;
    }

2.101. 对称二叉树 - 力扣(LeetCode)

思路1:我的思路类似于层次遍历,然后空节点依旧入队列。然后遍历的时候检查节点是否为空节点,vec就push(101),反之就push本来的数值。然后遍历完每一层后(size),检查vec数组是否是对称的。

    bool isSymmetric(TreeNode *root)
    {
        if (root == nullptr)
            return true;
        queue<TreeNode *> que;
        que.push(root->left);
        que.push(root->right);
        while (!que.empty())
        {
            int size = que.size();
            vector<int> vec;//默认空
            for (int i = 0; i < size; i++)
            {
                TreeNode *node = que.front();
                que.pop();
                if (node == nullptr)
                {
                    vec.push_back(101);
                }
                else
                {
                    vec.push_back(node->val);
                    que.push(node->left);
                    que.push(node->right);
                }
            }
            for (int i = 0, j = vec.size() - 1; i < j; i++, j--)
            {
                if (vec[i] != vec[j]){
                    return false;
                }
            }
        }

        return true;
    }

思路2:递归法,也就是说定义一个比较函数,分别传入left和right。然后分别在这两个分支上进行外侧节点和内侧节点的比较,即如图所示,深度为2的那一层的外侧的3和内侧的4。个人感觉还是比较喜欢迭代写法,不太喜欢递归写法...总感觉有点绕

    bool compare(TreeNode *left, TreeNode *right){
        //左节点为空,右节点不为空,不对称,return false
        //左不为空,右为空,不对称 return false 
        //左右都为空,对称,返回true
        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;
        else return compare(left->left, right->right) && compare(left->right, right->left);
    }
    bool isSymmetric(TreeNode *root)
    {
       if(root == nullptr)
            return true;
        return compare(root->left, root->right);
    }

思路3:迭代法,思路与上面类似,但是与层次遍历不太相同。层次遍历只有一个指针,然后按顺序遍历。但是这个属于设置左右孩子两根指针,然后首先判断外侧,再判断内侧。
最后push的顺序要注意!!!首先left->left,right->right(外侧),再left->right,right->left(内侧)

 bool isSymmetric(TreeNode *root)
    {
        if (root == nullptr)
            return true;
        queue<TreeNode *> que;
        que.push(root->left);
        que.push(root->right);
        while (!que.empty())
        {
            TreeNode *left = que.front();
            que.pop();
            TreeNode *right = que.front();
            que.pop();
            if (left == nullptr && right == nullptr) // 都为空,即对称的
                continue;
            if (left == nullptr || right == nullptr || left->val != right->val)
                return false;
            que.push(left->left);
            que.push(right->right);
            que.push(left->right);
            que.push(right->left);
        }
        return true;
    }

3.104. 二叉树的最大深度 - 力扣(LeetCode)

1.思路1:迭代法,就是层次遍历,每遍历一层,res++;

    int maxDepth(TreeNode* root) {
        if(root == nullptr)
            return 0;
        queue<TreeNode*> que;
        que.push(root);
        int res = 0;
        while(!que.empty()){
            int size = que.size();
            res++;
            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;
    }

2.思路2:递归法,下面用的是后序遍历(左右中)。后序遍历求的是高度。

   int getDepth(TreeNode *root)
    {
        if (root == nullptr)
            return 0;
        int left = getDepth(root->left);
        int right = getDepth(root->right);
        int result = 1 + max(left, right);
        return result;
    }

    int maxDepth(TreeNode *root)
    {
        return getDepth(root);
    }

3.思路3:递归法,下面用的是前序遍历(中左右)。前序遍历求的是深度。

 int result;
    void getDepth(TreeNode *root, int depth)
    {
        result = depth > result ? depth : result; // 中
        if (root->left == nullptr && root->right == nullptr)
        {
            return;
        }
        if (root->left) // 左
        {
            depth++;
            getDepth(root->left, depth);
            depth--; // 回溯,深度-1
        }
        if (root->right) // 右
        {
            depth++;
            getDepth(root->right, depth);
            depth--; // 回溯,深度-1
        }
        return;
    }

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

注意⚠️:getDepth(root->right, depth+1);和getDepth(root->right, depth++);不一样

4.111. 二叉树的最小深度 - 力扣(LeetCode)

1.思路1:前序遍历(中左右)

    int result;
    void get_depth(TreeNode *node, int depth)
    {
        if (node->left == nullptr && node->right == nullptr)
        {
            result = min(result, depth);
            return;
        }
        if (node->left)
        {
            depth++;
            get_depth(node->left, depth);
            depth--;
        }
        if (node->right)
        {
            depth++;
            get_depth(node->right, depth);
            depth--;
        }
        return;
    }
    int minDepth(TreeNode *root)
    {
        result = INT_MAX;
        if (root == nullptr)
            return 0;
        get_depth(root, 1);
        return result;
    }

2.思路2:迭代法

int minDepth(TreeNode* root) {
        if (root == NULL) 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* node = que.front();
                que.pop();
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
                if (!node->left && !node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出
                    return depth;
                }
            }
        }
        return depth;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值