zoukankan      html  css  js  c++  java
  • 100. Same Tree

    1. 问题描述

    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

    Tags: Tree Depth-first Search
    /**
    * Definition for a binary tree node.
    * struct TreeNode {
    * int val;
    * TreeNode *left;
    * TreeNode *right;
    * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    * };
    */

    2. 解题思路


    3. 代码

     1 class Solution {
     2 public:
     3     bool isSameTree(TreeNode* p, TreeNode* q)
     4     {
     5         if (NULL == p && NULL == q)
     6         {
     7             return true;
     8         }
     9 
    10         if (NULL != p && NULL != q)
    11         {
    12             if (p->val == q->val)
    13             {
    14                 return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    15             }
    16             else
    17             {
    18                 return false;
    19             }
    20         }
    21         return false;
    22     }
    23 #pragma region 非递归方式
    24     bool isSameTree_1(TreeNode *p, TreeNode *q) 
    25     {
    26         if(!isSameNode(p, q))
    27             return false;
    28         if(!p && !q)
    29             return true;
    30 
    31         queue<TreeNode*> lqueue;
    32         queue<TreeNode*> rqueue;
    33         lqueue.push(p);
    34         rqueue.push(q);
    35         while(!lqueue.empty() && !rqueue.empty())
    36         {
    37             TreeNode* lfront = lqueue.front();
    38             TreeNode* rfront = rqueue.front();
    39 
    40             lqueue.pop();
    41             rqueue.pop();
    42 
    43             if(!isSameNode(lfront->left, rfront->left))
    44                 return false;
    45             if(lfront->left && rfront->left)
    46             {
    47                 lqueue.push(lfront->left);
    48                 rqueue.push(rfront->left);
    49             }
    50 
    51             if(!isSameNode(lfront->right, rfront->right))
    52                 return false;
    53             if(lfront->right && rfront->right)
    54             {
    55                 lqueue.push(lfront->right);
    56                 rqueue.push(rfront->right);
    57             }
    58         }
    59         return true;
    60     }
    61     bool isSameNode(TreeNode* p, TreeNode *q)
    62     {
    63         if(!p && !q)
    64             return true;
    65         if((p && !q) || (!p && q) || (p->val != q->val))
    66             return false;
    67         return true;
    68     }
    69 #pragma endregion
    70 };

    4. 反思

  • 相关阅读:
    ES6模块
    遍历数组和对象的方法
    JVM知识(六):linux环境下查看JVM内存大小
    WebSocket实时消息推送
    SpringBoot中基于Mybatis多数据源操作
    浅谈Redis中的雪崩和穿透及击穿。
    Hibernate与Mybatis的区别
    Java动态代理和反射机制
    JSON对象和JSON字符串的区别
    JVM知识(五):垃圾回收算法
  • 原文地址:https://www.cnblogs.com/whl2012/p/5596735.html
Copyright © 2011-2022 走看看