当前位置: 首页 > article >正文

leetCode 100. 相同的树 和 leetCode 101. 对称二叉树 和 110. 平衡二叉树 和 199. 二叉树的右视图

1.leetCode 100. 相同的树

C++代码:

class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(p == nullptr || q == nullptr) return p==q;
        return p->val == q->val && isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
    }
};

Python代码: 

class Solution:
    def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
        if p is None or q is None:
            return p is q
        return p.val == q.val and self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)

2.leetCode 101. 对称二叉树
力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

C++代码: 

class Solution {
public:
    bool isSameTree(TreeNode* p,TreeNode* q) {
        if(p == nullptr || q == nullptr) return p==q;
        return p->val == q->val && isSameTree(p->left,q->right) && isSameTree(p->right,q->left);
    }
    bool isSymmetric(TreeNode* root) {
        if(root==nullptr) return true;
        TreeNode* p = root->left;
        TreeNode* q = root->right;
        return isSameTree(p,q);
    }
};

 Python代码: 

class Solution:
    def isSameTree(self,p:Optional[TreeNode],q:Optional[TreeNode]) -> bool:
        if p is None or q is None:
            return p is q
        return p.val == q.val and self.isSameTree(p.left,q.right) and self.isSameTree(p.right,q.left)

    def isSymmetric(self, root: Optional[TreeNode]) -> bool:
        return self.isSameTree(root.left,root.right)

3.leetCode 110.平衡二叉树
https://leetcode.cn/problems/balanced-binary-tree/description/

C++代码:

class Solution {
public:
    int getHeight(TreeNode* root) {
        if(root==nullptr) return 0;
        int LH = getHeight(root->left);
        if(LH == -1) return -1;
        int RH = getHeight(root->right);
        if(RH == -1 or abs(RH-LH) > 1) return -1;
        return max(LH,RH)+1;
    }
    bool isBalanced(TreeNode* root) {
        return getHeight(root)!=-1;
    }
};

Python代码:

class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        def get_height(node):
            if node is None: 
                return 0
            LH = get_height(node.left)
            if LH == -1:
                return -1
            RH = get_height(node.right)
            if RH == -1 or abs(RH-LH) > 1:
                return -1
            return max(LH,RH)+1
        return get_height(root)!=-1

4.leetCode 199. 二叉树的右视图
https://leetcode.cn/problems/binary-tree-right-side-view/description/

C++代码: 

class Solution {
public:
    vector<int> ans;
    void dfs(TreeNode* node,int depth) {
        if(node==nullptr) return;
        if(depth == ans.size()) ans.push_back(node->val);
        dfs(node->right,depth+1);
        dfs(node->left,depth+1);
    }
    vector<int> rightSideView(TreeNode* root) {
        dfs(root,0);
        return ans;
    }
};

Python代码: 

class Solution:
    def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
        ans = []
        def f(node,depth):
            if node is None:
                return 
            if depth == len(ans):
                ans.append(node.val)
            f(node.right,depth+1)
            f(node.left,depth+1)
        f(root,0)
        return ans

参考和推荐文章、视频:

如何灵活运用递归?【基础算法精讲 10】_哔哩哔哩_bilibili

100. 相同的树 https://leetcode.cn/problems/same-tree/solutions/2015056/ru-he-ling-huo-yun-yong-di-gui-lai-kan-s-empk/

101. 对称二叉树 https://leetcode.cn/problems/symmetric-tree/solutions/2015063/ru-he-ling-huo-yun-yong-di-gui-lai-kan-s-6dq5/

110. 平衡二叉树 https://leetcode.cn/problems/balanced-binary-tree/solutions/2015068/ru-he-ling-huo-yun-yong-di-gui-lai-kan-s-c3wj/

199. 二叉树的右视图 https://leetcode.cn/problems/binary-tree-right-side-view/solutions/2015061/ru-he-ling-huo-yun-yong-di-gui-lai-kan-s-r1nc/ 


http://www.kler.cn/news/148738.html

相关文章:

  • 【Kotlin】高阶函数和Lambda
  • GitLab 登录中,LDAP和 Standard 验证有什么区别
  • C++:C++11新特性---右值引用
  • Notion for Mac:打造您的专属多功能办公笔记软件
  • 小狐狸ChatGPT付费创作系统V2.3.4独立版 +WEB端+ H5端最新去弹窗授权
  • 【开题报告】基于uniapp的瑜伽学习交流小程序的设计与实现
  • 近五年—中国十大科技进展(2018年—2022年)
  • Spring Cloud 版本升级记:OpenFeignClient与Gateway的爱恨交织
  • 【C++ Primer Plus学习记录】while循环
  • 批量将本地N个英文Html文档进行中文翻译-操作篇
  • 移动应用开发介绍及iOS方向学习路线(HUT移动组版)
  • OpenCV | 模版匹配
  • SpringCloudAlibaba整合Gateway实现网关
  • stm32 TIM
  • 【Docker项目实战】使用Docker部署Plik临时文件上传系统
  • 车载以太网-DHCP
  • Python中的datetime库
  • 硬件工程师助理怎么买器件
  • 08-学成在线项目中统一异常处理的规范
  • C# WPF上位机开发(乘法计算小软件)
  • 面试:MyBatis问题
  • 论文阅读——DDeP(cvpr2023)
  • 2017年五一杯数学建模C题宜居城市问题值解题全过程文档及程序
  • Web前端开发技术:图像与多媒体文件
  • OpenCvSharp从入门到实践-(05)通道
  • ABAP算法 模拟退火
  • c语言 / 指针错误的几种情况
  • 从 0 到 1 开发一个 node 命令行工具
  • 【Linux驱动开发】环境搭建Linux驱动开发环境
  • EDA实验-----正弦信号发生器的设计(Quartus II )