【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+selenium+pychar安装
python3.5(在百度输入python进入python官网-downloads-Windows-然后选择要下载的版本(可执行安装包,若电脑为32位的选择×86,若为64的选择×64)) selen ...
- Java 数据表映射
一对多映射 class Province { //每一个类就相当于数据库中的一个表: private int pid ; private String name ; private City citi ...
- python any和all
摘自<流畅的Python> all 和 any 也是内置的归约函数. all(iterable) 如果 iterable 的每个元素都是真值,返回 True:all([]) 返回 True ...
- SQL操作Json数据
转载自: http://blog.csdn.net/yapingxin/article/details/16913275 有小改动.. 支持复杂结构的使用.. 使用Parent_ID来对应Object ...
- Python学习--猫眼电影TOP100榜单抓取
import requests import re import json import time def get_one_page(url): headers={'User-Agent':'Mozi ...
- oracle dump的使用心得
使用DS开发的时候,有的时候会遇到一个问题:数据库层面定义的空格与DS自已定义的空格概念不一致,导致生成的数据会有一定的问题. 举例来说: 在数据库里面定义CHAR(20),如果插入的字符不足20的时 ...
- 对Map的一些总结
1:Map接口. Collection体系中存储的是单个元素,单身汉,而Map中存储的是2个元素,存储的是成对的元素. Map和Collection是没有联系的!!不要以为Map是Collection ...
- flyway-Maven插件-configuration节点配置详解
<configuration> <driver>org.hsqldb.jdbcDriver</driver> <url>jdbc:hsqldb:file ...
- CSS禁止滚动条
CSS禁止滚动条的方法: 1.完全隐藏 在<boby>里加入scroll="no",可隐藏滚动条: <boby scroll="no"> ...
- css3中比较少用到的属性记录
letter-spacing 属性 支持:所有浏览器都支持 letter-spacing 属性. letter-spacing 属性增加或减少字符间的空白(字符间距). 该属性定义了在文本字符框之间插 ...