zoukankan      html  css  js  c++  java
  • leetcode Same Tree

    Given two binary trees, write a function to check if they are equal or not.

    Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

    Subscribe to see which companies asked this question

    做这道题的时候犯了两个错误,(1)刚开始的竟然直接用p==q,p和q是指针,只要指的不是一块内存,应该返回的都是false啊。(2)后来*p==*q,可是并没有运算符==重载,这样也是没有结果的。

    后来发现只要value值一样就可以了,事实上体现出来的就是两个节点的value值,【递归】什么的真的很好用。为以前没有好好学递归感到遗憾啊。easy程度的题的确可以一天刷好几道。应该安排的是时间不是量。因为随着能力的提高,程度不一样了,量也应该减少,减肥应该也是这样的,【吸取教训】

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     bool isSameTree(TreeNode* p, TreeNode* q) {
    13         if(p==NULL&&q==NULL) return true;
    14         if(p!=NULL&&q!=NULL){
    15         if(p->val==q->val){
    16             if(isSameTree(p->right,q->right)&&isSameTree(p->left,q->left)) return true;
    17         }
    18         }
    19         return false;
    20     }
    21 };
  • 相关阅读:
    设计模式 创建型 抽象工厂
    mysql 案例 ~ 分析执行完的大事务
    mysql 查询优化案例汇总
    mysql 原理 ~ innodb恢复机制
    mysql 原理~ 乐观锁和悲观锁
    mysql 原理 ~ 常规锁
    mysql 5.7 ~ 新特性
    mysql 原理 ~ checkpoint
    mysql原理~undo管理
    mysql原理~undo
  • 原文地址:https://www.cnblogs.com/LUO77/p/4956241.html
Copyright © 2011-2022 走看看