代码随想录刷题第14天

二叉树算法实现:层序遍历、翻转与对称性检查

代码随想录刷题第14天

二叉树的层序遍历

/*
 * @lc app=leetcode.cn id=102 lang=cpp
 *
 * [102] 二叉树的层序遍历
 */

// @lc code=start
/**
 * 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) {}
 * };
 */
#include <vector>
#include <iostream>
using namespace std;
// struct TreeNode {
//       int val;
//       TreeNode *left;
//       TreeNode *right;
//       TreeNode() : val(0), left(nullptr), right(nullptr) {}
//       TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
//   };
class Solution {
public:
    void transation(TreeNode* root,int depth,vector<vector<int>> &result){
        if (root == nullptr)
        {
            return ;
        }
        if (result.size() == depth)
        {
            result.push_back(vector<int>());
        }
        result[depth].push_back(root->val);
        transation(root->left,depth+1,result);
        transation(root->right,depth+1,result);
    }
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> result;
        //int depth = 0;
        transation(root,0,result);
        return result;
    }
};
// @lc code=end


递归法,寻找递归条件,然后每一层插入一个数组,相当于深度优先遍历然后插入,从而变成纵向遍历~

翻转二叉树

/*
 * @lc app=leetcode.cn id=226 lang=cpp
 *
 * [226] 翻转二叉树
 */

// @lc code=start
/**
 * 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) {}
 * };
 */

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// struct TreeNode {
//       int val;
//       TreeNode *left;
//       TreeNode *right;
//       TreeNode() : val(0), left(nullptr), right(nullptr) {}
//       TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
//   };

class Solution {
public:
    void transation(TreeNode* root){
        if (root == nullptr)
        {
            return ;
        }
        swap(root->left,root->right);
        transation(root->left);
        transation(root->right);
        
    }
    TreeNode* invertTree(TreeNode* root) {
        transation(root);
        return root;
    }
};
// @lc code=end


简单的翻转左右孩子!

对称二叉树

/*
 * @lc app=leetcode.cn id=101 lang=cpp
 *
 * [101] 对称二叉树
 */

// @lc code=start
/**
 * 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) {}
 * };
 */

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// struct TreeNode {
//       int val;
//       TreeNode *left;
//       TreeNode *right;
//       TreeNode() : val(0), left(nullptr), right(nullptr) {}
//       TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
//   };


class Solution {
public:
    bool transation(TreeNode* left,TreeNode* right){
        if (left == NULL&& right == NULL)
        {
            return true;
        }else if (left != NULL&& right == NULL)
        {
            return false;
        }else if (left == NULL&& right!= NULL)
        {
            return false;
        }else if (left->val  != right->val)
        {
            return false;
        }
        bool out =  transation(left->left,right->right);
        bool in = transation(left->right,right->left);
        bool final = out && in;
        return final;
    }


    bool isSymmetric(TreeNode* root) {
        if (root == nullptr)
        {
            return true;
        }
        
        bool result = transation(root->left,root->right);
        return result;
    }
};
// @lc code=end


### 代码随想录中的Python答案 对于希望在《代码随想录》中查找Python的答案,该资源提供了详细的算法目解析以及对应的解决方案。具体到不同类型的目,《代码随想录》不仅提供了解决方案还深入讲解了背后的原理。 针对数组类问,在处理`sortedSquares`函数时采用了一种更高效的方法来解决平方排序的问[^1]: ```python class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: result = [0] * len(nums) # 初始化结果列表 left, right, pos = 0, len(nums) - 1, len(nums) - 1 while left <= right: if abs(nums[left]) > abs(nums[right]): result[pos] = nums[left] ** 2 left += 1 else: result[pos] = nums[right] ** 2 right -= 1 pos -= 1 return result ``` 此方法利用双指针技术有效地减少了不必要的计算开销,并且保持了时间复杂度为O(n),而不需要额外的空间除返回的结果外[^2]。 当涉及到动态规划问初始化时,则依据具体情况决定如何设置初始状态。例如,当面对含有负数值的情况时,非零索引位置应被设为负无穷大以确保后续比较逻辑正确无误[^4]。 另外,《代码随想录》也涵盖了更多高级主如单调栈的应用实例,这有助于理解特定场景下的最优解法[^5]。 #### 注意事项 为了更好地理解和应用这些解答建议读者仔细阅读原文档内的解释说明部分,因为那里包含了实现细节背后的重要概念和技巧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值