• 【Leetcode】【Easy】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.

    递归的解法:

    用树递归的思想,将两个树以结点作为比较单位,只关注对当前位置结点的操作(是否都存在此结点,存在时值是否相同)。 

    注意:

    1、只关注单个结点,结点的孩子交给递归去做,这样可以最简化逻辑;

    2、结点是否存在、结点值是否相等、结点是否有孩子,是三个不同的概念(“结点是否有孩子”的概念本题中未体现);

     1 /**
     2  * Definition for binary tree
     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 
    11 class Solution {
    12 public:
    13     bool isSameTree(TreeNode *p, TreeNode *q) {
    14         if (!p && !q)
    15             return true;
    16         
    17         if ((p && !q) || (!p && q) || (p->val != q->val))
    18             return false;
    19             
    20         return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    21     }
    22 };

    迭代的解法:

    引用先序/中序/后序/按层等遍历二叉树的思想,用堆栈(数组)存放遍历到的结点,进栈出栈的同时进行比较。

    中序遍历(其他方法类似):

     1 /**
     2  * Definition for binary tree
     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         vector<TreeNode *> stackP;
    14         vector<TreeNode *> stackQ;
    15         TreeNode *currentP = p;
    16         TreeNode *currentQ = q;
    17         
    18         while (currentP || stackP.size()) {
    19             if (!currentP && !currentQ) {
    20                 currentP = stackP.back();
    21                 currentQ = stackQ.back();
    22                 stackP.pop_back();
    23                 stackQ.pop_back();
    24                 currentP = currentP->right; 
    25                 currentQ = currentQ->right;
    26                 continue;
    27             }
    28             
    29             if ((!currentP && currentQ) || (currentP && !currentQ) ||
    30                 (currentP->val != currentQ->val))
    31                 return false;
    32             
    33             if (currentP->val == currentQ->val) {
    34                 stackP.push_back(currentP);
    35                 stackQ.push_back(currentQ);
    36                 currentP = currentP->left;
    37                 currentQ = currentQ->left;
    38             }
    39         }
    40         
    41         if (!currentQ) {
    42             return true;
    43         } else {
    44             return false;
    45         }
    46         
    47     }
    48 };

    附录:

    C++中vector、stack、queue用法和区别

    递归/非递归遍历二叉树

  • 相关阅读:
    Oracle 中 varchar2(N) 与 varchar2(N char) 的区别
    EXP-00008: 遇到 ORACLE 错误 1455
    服务器重装Windows Server2008 R2操作系统
    h5页面自定义主题色(vue)
    初窥vue3.0
    ElasticSearch学习笔记_1
    mysql索引的使用
    什么时候使用视图
    Latex使用手册记录
    最大熵模型理论及NLP应用总结
  • 原文地址:https://www.cnblogs.com/huxiao-tee/p/4107031.html
Copyright © 2020-2023  润新知