【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、结点是否存在、结点值是否相等、结点是否有孩子,是三个不同的概念(“结点是否有孩子”的概念本题中未体现);
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if (!p && !q)
return true; if ((p && !q) || (!p && q) || (p->val != q->val))
return false; return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
迭代的解法:
引用先序/中序/后序/按层等遍历二叉树的思想,用堆栈(数组)存放遍历到的结点,进栈出栈的同时进行比较。
中序遍历(其他方法类似):
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
vector<TreeNode *> stackP;
vector<TreeNode *> stackQ;
TreeNode *currentP = p;
TreeNode *currentQ = q; while (currentP || stackP.size()) {
if (!currentP && !currentQ) {
currentP = stackP.back();
currentQ = stackQ.back();
stackP.pop_back();
stackQ.pop_back();
currentP = currentP->right;
currentQ = currentQ->right;
continue;
} if ((!currentP && currentQ) || (currentP && !currentQ) ||\
(currentP->val != currentQ->val))
return false; if (currentP->val == currentQ->val) {
stackP.push_back(currentP);
stackQ.push_back(currentQ);
currentP = currentP->left;
currentQ = currentQ->left;
}
} if (!currentQ) {
return true;
} else {
return false;
} }
};
附录:
C++中vector、stack、queue用法和区别
递归/非递归遍历二叉树
【Leetcode】【Easy】Same Tree的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解 ...
- 【leetcode刷题笔记】Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- 【leetcode刷题笔记】Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【leetcode刷题笔记】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
随机推荐
- 我的Python升级打怪之路【三】:Python函数
函数 在函数之前,我们一直遵循者:面向过程编程,即:根据业务逻辑从上到下实现功能,开发过程中最常见的就是粘贴复制.代码就没有重复利用率. 例如:有好多的重复的代码 if 条件: 发送指令 接收结果 e ...
- gradle简单配置跟模块依赖
参考文章: https://www.cnblogs.com/lykbk/p/erwerwerwerwerwerwe.html https://www.cnblogs.com/jiangxiaoyaob ...
- css3中outline切换动画效果
今天刚看了篇文章<纯CSS实现的outline切换transition动画效果> 里面的效果研究了一下,下图为实现时的效果 代码如下: <!DOCTYPE html> < ...
- spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue
一: ThreadPoolTaskExecutor是一个spring的线程池技术,查看代码可以看到这样一个字段: private ThreadPoolExecutor threadPoolExecut ...
- 80端口被系统占用,无法kill
哎,最近郁闷了,一直想用80端口配到APache上,可是老是找不到原因,Nginx 都停掉了,也没有装IIS,也没发出别的程序占用着80端口,又不想换到别的端口 一定要找到问题,坚持!!! 用cmd窗 ...
- Hibernate 多对一映射
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBL ...
- Vim常用插件——前端开发工具系列
作为一名开发者,应该对编辑器之神Vim与神之编辑器Emacs有所耳闻吧.编辑器之战的具体细节有兴趣的童鞋可以google之. Vim最大的特点是打开速度快,功能强大,一旦掌握了其中的命令,编程过程双手 ...
- 利用kvo对集合进行操作
利用kvo对集合进行操作 NSLog(@"其他学生的成绩%@", [array valueForKeyPath:@"point"]); NSLog(@" ...
- swagger2配置和使用
1.导入swagger2 <dependency> <groupId>io.springfox</groupId> <artifactId>spring ...
- Vue中的静态资源管理(src下的assets和static文件夹的区别)
### 你可能注意到了我们的静态资源共有两个目录src/assets和static/,你们它们之间有怎样的区别呢? 资源打包 为了回答这个问题,我们需要了解webpack是如何处理静态资源的. 在所有 ...