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.

思路一:前序递归的思想。时间复杂度O(n),空间复杂度O(logN)

  

 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         if (!p && !q) return true;
14         if (!p || !q ) return false;
15
16         return (p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
17     }
18 };

思路二:层次遍历的思想。时间复杂度O(n), 空间复杂度O(logN)

  

 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         queue<TreeNode *> s;
14         s.push(p);
15         s.push(q);
16
17         while (!s.empty()) {
18             p = s.front();
19             s.pop();
20             q = s.front();
21             s.pop();
22             if (!p && !q) continue;
23             if (!p || !q) return false;
24             if (p->val != q->val) return false;
25
26             s.push(p->left);
27             s.push(q->left);
28             s.push(p->right);
29             s.push(q->right);
30         }
31
32         return true;
33     }
34 };

转载于:https://www.cnblogs.com/vincently/p/4232467.html

[LeetCode] Same Tree相关推荐

  1. [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

    目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...

  2. Leetcode | Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  3. LeetCode N-ary Tree Level Order Traversal(bfs)

    问题:给出一个n叉树,输出层次遍历 思路:bfs 具体代码参考: https://github.com/wuli2496/OJ/tree/master/LeetCode/N-ary%20Tree%20 ...

  4. LeetCode Binary Tree Right Side View(搜索)

    问题:给出一个二叉树,要求输出右视图 思路:因为要求输出右视图.可以考虑使用深度优先搜索或者 广度优先搜索. 使用深度优先搜索时,以非递归形式,将左右子树入栈,同时使用哈希表记录深度与对应右视图的值. ...

  5. LeetCode Binary Tree Preorder Traversal(二叉树的前序遍历)

    问题:给出一个二叉树,输出前序遍历 思路: 自顶向下遍历过程中,将当前结点的值加入到list中,然后处理左.右子树 具体代码参考: https://github.com/wuli2496/OJ/tre ...

  6. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  7. [LeetCode] Binary Tree Postorder题解

    Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...

  8. [Leetcode] Binary Tree Maximum Path Sum

    这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...

  9. [LeetCode] N-ary Tree Postorder Traversal N叉树的后序遍历

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

  10. [leetcode] Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

最新文章

  1. 【EventBus】EventBus 源码解析 ( 注册订阅者总结 | EventBus 注册订阅者流程梳理 )
  2. 计算机考研一些常纹面试知识,2020年光学工程考研复试真题和技巧
  3. [BZOJ2125]最短路(圆方树DP)
  4. C# 规则引擎RulesEngine
  5. php源码查找替换,php 替换模板中的 PHP源码标签字符方法
  6. python process_Python Process/Thread 概念整理
  7. top-1和top-5正确率与错误率以及目标检测评价指标
  8. 用英语描述计算机操作,操作系统的英文介绍
  9. 人均34万,腾讯为3300名员工发11亿红包;B站回应大会员补偿会自动续费;​小米销量超苹果跻身全球第二|极客头条...
  10. 机器学习之——神经网络模型
  11. 四川省盐业学校九五计算机,2020年四川省盐业学校招生录取分数线
  12. PyMySQL和MySQLdb的区别
  13. 携程、快手、平安银行、哈啰出行是怎么落地数据治理和DataOps的?丨DAMS峰会...
  14. php ziparchive类,PHP Zip ZipArchive 类_编程学问网
  15. 系统软件设计原则_软件系统设计原则
  16. html实现word分页符,word分页-解析Word——自动分页符与手动分页符
  17. python模板是什么意思_python – 这个模板中的正确包含路径是什么?
  18. linux 使用c语言如何获取网关地址
  19. 昨晚,谷歌发布了一个可怕的人工智能!
  20. TypeScript ... 解释三个点的含义

热门文章

  1. JS中location对象使用
  2. 借助开源工具高效完成Java应用的运行分析
  3. 面试高频题: LRU缓存机制实现
  4. Linux系统中的函数
  5. 比特币多重签名机制使用篇
  6. python六十四: 迭代器协议
  7. c:数据结构-线性表
  8. 剑指Offer 56 数组中数字出现的次数
  9. scipy的一些函数名
  10. js复制网站文字追加网站来源,网站版权