zoukankan      html  css  js  c++  java
  • 【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用法和区别

    递归/非递归遍历二叉树

  • 相关阅读:
    字符串匹配常见算法(BF,RK,KMP,BM,Sunday)
    JSP基本语法总结【1】(jsp工作原理,脚本元素,指令元素,动作元素)
    JUnit【1】断言用法之assertEquals/True/False/ArrayEquals
    软件测试基础配置
    前端入门20-JavaScript进阶之异步回调的执行时机
    前端入门19-JavaScript进阶之闭包
    前端入门18-JavaScript进阶之作用域链
    前端入门17-JavaScript进阶之作用域
    前端入门16-JavaScript进阶之EC和VO
    前端入门15-JavaScript进阶之原型链
  • 原文地址:https://www.cnblogs.com/huxiao-tee/p/4107031.html
Copyright © 2011-2022 走看看