【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 ...
随机推荐
- Unity 动画 命名
unity标准的动画命名格式是 ?模型名@动画名 如boss1@idle ,这样导入进来之后unity会自动给这个动画命名为idle而不是Take 001.
- SPRING中的线程池ThreadPoolTaskExecutor(转)
转自:https://blog.csdn.net/zhanglongfei_test/article/details/51888433 一.初始化 1,直接调用 ThreadPoolTaskExecu ...
- 【随笔】使用apt-spy来更新你的debian源
debian什么最方便,当然是用apt-get intsall 命令来安装软件了.使用apt-get什么最重要,自然是下载源了. debian版本自带的源肯定不是最快的,考虑到个人所处的位置.网速等方 ...
- 深入理解JavaScript系列(41):设计模式之模板方法
介绍 模板方法(TemplateMethod)定义了一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 模板方法是一种代码复用的 ...
- 【关于eclipse的一些自己常用的插件】
代码自动走查: sonarlnt:
- easyui导出当前datagrid数据(含表头)
JS代码 //导出当前DataGrid数据 function doExportCommon() { var list = getCheckedRowCommon(); var exportList = ...
- webpack的学习感悟
https://github.com/webpack/webpack webpack gethub地址. http://webpack.github.io/ webpack 官网 前言 we ...
- [javaEE] 开源数据库连接池
一些开源组织提供了数据源的独立实现: DBCP数据库连接池 C3P0数据库连接池 Apache Tomcat内置的连接池 DBCP连接池 apache提供的连接池实现,需要导入common-dbcp. ...
- Java 基础(1)—— 开始前的准备
虽然学习 Java 已有一年多,但是从来没有仔细总结或者复习过.于是准备借用博客来进行一波学习记录.从头开始,学习 Java. Java 介绍 生产公司:Sun Microsystems 公司(200 ...
- requset获取post提交的请求参数
1.请求体的内容通常是通过post来提交的,格式是 username=zhansan&password=123&hobby=football||&hobby=basketbal ...