【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 ...
随机推荐
- Unix_JDK安装及配置
CentOS 下过程 JDK 在 CentOS 和 Ubuntu 下安装过程是一样的,所以这里不再讲 Ubuntu 系统下的安装 JDK 1.8 下载 此时(20170906)最新版本:jdk-8u1 ...
- btn按钮事件
1.用Delegate 和 Event 来定义一个通用类来处理事件 (观察者模式) using System.Collections; using System.Collections.Generic ...
- Ubuntu系统下安装并配置hive-2.1.0
说在前面的话 默认情况下,Hive元数据保存在内嵌的Derby数据库中,只能允许一个会话连接,只适合简单的测试.实际生产环境中不使用,为了支持多用户会话, 则需要一个独立的元数据库,使用MySQL作为 ...
- Redis的安装(CentOS 7下)
redis的官网,www.redis.io 1. 先下载 redis: wget http://download.redis.io/releases/redis-3.0.3.tar.gz 2. 解压: ...
- The “SignFile” task was not given a value for the required parameter “CertificateThumbprint”的一个简单的解决方法
这个只是其中一种解决方法,而且不是万能的 1. 由提示内容可以看出,这个一个 sign(认证)的问题, 在出现这个问题的项目上,鼠标右键,选择properties,然后选择signing. 2. 选择 ...
- 登录mysql 报 Access denied for user 'root'@'localhost' 错误
安装mysql后登录提示:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password:yes) 解决如下 ...
- Python基础(3) - 数据类型:2字符串类型
Python字符串的表示有三种方法: 1.单引号(') >>>a = 'I love python. ' 2.双引号(") >>>a = " I ...
- 使用 Qt 获取 UDP 数据并显示成图片(2)
本文首发于 BriFuture 的 个人博客 在我的前一篇文章 使用 Qt 获取 UDP 数据并显示成图片 中,我讲了如何用 Python 模拟发送数据,如何在 Qt 中高效的接收 UDP 数据包并将 ...
- Windows Server 2008系统中IE8启用和禁用JS
Windows Server 2008系统中IE8默认是启用IE ESC(ie 增强)的,这样会导致该IE不支持JS,开启方法: 1.开始->管理工具->服务器管理器 2.点击服务器管理- ...
- Shell脚本之awk详解
一.基本介绍 1.awk: awk是一个强大的文本分析工具,在对文本文件的处理以及生成报表,awk是无可替代的.awk认为文本文件都是结构化的,它将每一个输入行定义为一个记录,行中的每个字符串定义为一 ...