leetcode - Same Tree
题目: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、比较两棵树是否相同,先比较根节点是否相同,再分别比较左子树和右子树是否相同,可以看出这是一个递归的过程,一般树的题目都会涉及递归
代码:
1 #include <stddef.h> 2 /* 3 struct TreeNode 4 { 5 int val; 6 TreeNode *left; 7 TreeNode *right; 8 TreeNode(int x) : val(x), left(NULL), right(NULL) {} 9 }; 10 */ 11 class Solution 12 { 13 public: 14 bool isSameTree(TreeNode *p, TreeNode *q) 15 { 16 //p,q均为空 17 if (p == NULL && q == NULL) 18 { 19 return true; 20 } 21 22 //p,q不为空时,p,q指向结点的值相同且p,q左右子树均相同,则说明p,q指向的树相同 23 if (p != NULL && q != NULL && p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right)) 24 { 25 return true; 26 } 27 28 return false; 29 } 30 };
View Code
按照惯例,到网上搜寻是否有更好的方法,发现大部分都是采用这种递归的思路,就这样吧
转载于:https://www.cnblogs.com/laihaiteng/p/3786077.html
leetcode - Same Tree相关推荐
- [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)
目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...
- 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. ...
- LeetCode N-ary Tree Level Order Traversal(bfs)
问题:给出一个n叉树,输出层次遍历 思路:bfs 具体代码参考: https://github.com/wuli2496/OJ/tree/master/LeetCode/N-ary%20Tree%20 ...
- LeetCode Binary Tree Right Side View(搜索)
问题:给出一个二叉树,要求输出右视图 思路:因为要求输出右视图.可以考虑使用深度优先搜索或者 广度优先搜索. 使用深度优先搜索时,以非递归形式,将左右子树入栈,同时使用哈希表记录深度与对应右视图的值. ...
- LeetCode Binary Tree Preorder Traversal(二叉树的前序遍历)
问题:给出一个二叉树,输出前序遍历 思路: 自顶向下遍历过程中,将当前结点的值加入到list中,然后处理左.右子树 具体代码参考: https://github.com/wuli2496/OJ/tre ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Postorder题解
Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...
- [Leetcode] Binary Tree Maximum Path Sum
这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...
- [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 ...
- [leetcode] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
最新文章
- JAVA入门级教学之(if语句)
- python装好了怎么启动车_【填空题】Python安装好后,可以直接在CMD命令行下输入( )命令, 可启动交互式编程,提示窗口如下:...
- caffeine 弱引用key的实现
- 计算机学院志愿服务活动 2013/12/23 爱心6+1,计算机学院赴淮南南站开展志愿服务活动...
- Spring MVC自动为对象注入枚举数据
- [SDUT](3468)广度优先搜索练习之神奇的电梯 ---BFS(图)
- 【韦东山嵌入式Linux】vi编辑器入门笔记
- 学习笔记-Matlab二维绘图
- 【LG-P1251】餐巾计划问题
- 企业邮箱出现问题我们该联系谁?联系邮箱是什么?
- 数学笔记27——极坐标下的面积
- 正则-完美的身份证以及真实姓名
- 自动驾驶路侧感知定位技术条件
- mysql .myd_关于mysql 删除数据后(.MYD,MYI)物理空间未释放
- 为什么那么多程序员害怕Python?
- python如何计算平方_Python lmfit如何计算R平方?
- java计算平面度,计算产品面的平面度和平行度(C#代码)
- span从入门到精通1 第三方工具类GifDrawable
- 雷林鹏分享:Ruby 字符串(String)
- 在win10/win7 64位系统上安装 hp laserjet 1015对应的打印机驱动
热门文章
- Fast RCNN 训练自己的数据集(3训练和检测)
- 最大似然估计的一个示例
- Sublime P4语法高亮设置
- [翻译][1.4.2]Flask-Admin入门介绍
- seo策略从5方面下手
- Sun JDK: Hashmap.get非法使用的挂起分析
- Oracle中的date与timestamp
- springboot启动出错,
- spring的自生一个bug
- 被coding.net坑了, Git 动态Pages要停掉...