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.
给定两个二叉树,判断是否完全相同。
若两个二叉树完全相同,则每个节点的左子树和右子树必然都相同。
可以采用递归方式,从根节点开始:
/**
* 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 == NULL && q == NULL)
return true;
else if(p == NULL || q == NULL)
return false; if(p->val != q->val)
return false; bool left = isSameTree(p->left,q->left);
bool right = isSameTree(p->right,q->right); return left && right;
}
};
Same Tree 比较两个二叉树是否完全相同的更多相关文章
- same tree(判断两颗二叉树是否相等)
Input: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] Output: true Example 2: Input: 1 1 / \ 2 2 [1,2], [1,nul ...
- LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- LeetCode 617. Merge Two Binary Tree (合并两个二叉树)
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- 【遍历二叉树】08判断两个二叉树是否相同【Same Tree】
迭代版本用的是二叉树的DFS,中的root->right->left +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- Invert a binary tree 翻转一棵二叉树
Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4 / \ 2 7 / \ / \ 1 3 6 9翻转后: 4 / \ 7 ...
- leetcode算法题2: 合并两个二叉树。递归,如何切入并保持清醒?
/* Given two binary trees and imagine that when you put one of them to cover the other, some nodes o ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- 17.Merge Two Binary Trees(合并两个二叉树)
Level: Easy 题目描述: Given two binary trees and imagine that when you put one of them to cover the ot ...
- 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】
[145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...
随机推荐
- Collections.singletonList方法的使用
方法注释 /** * Returns an immutable list containing only the specified object. * The returned list is se ...
- C#控制台画图形
static void Main(string[] args) { //九九乘法 Console.WriteLine("九九乘法口诀"); ; i <= ; i++) { ; ...
- centos7嵌入式环境搭建
1. 在网上搜索下载交叉编译器arm-linux-gcc文件,我下载的是:arm-2014.05-29-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 ...
- STM32F103RE引脚功能整理
- (转)基于OpenStack构建企业私有云(1)实验环境准备
原文:https://www.unixhot.com/article/407 https://www.cnblogs.com/kevingrace/p/5707003.html-----完整部署Cen ...
- 关于c#中”ref”和”out”关键字的一些理解
一. 综述(本文内容大部分来自网络,经本人整理而成,仅供学习参考,不免理解错误,欢迎批评指正) 在c#中,方法的参数传递有四种类型: (1) 传值参数(by value) 传值参数无需额外的修饰符.传 ...
- 微服务Kong(二)——快速入门
在本节中,您将学习如何管理您的KONG实例.首先,我们将指导您如何启动Kong,以便您能访问KONG的RESTful形式的管理界面,您可以通过它来管理您的API,consumers等.通过管理型API ...
- git删除远程主机没有的tag
可以先删除所有本地tag,然后再拉取远程上的tag git tag -l | xargs git tag -d git fetch --tags 其他方法以及查询tag的命令请见:Remove loc ...
- oracle:存储过程和触发器
存储过程(stored procedure) :可以看作带名字的pl/sql程序块:通过名字调用执行:可以带参数或不带参数. 触发器(trigger):通过事件触发执行,可看成特殊类型的存储过程. 下 ...
- mysql导入外部.sql文件时错误
当前环境: 操作系统:windows 7 mysql版本:5.5.36 MySQL Community Server (GPL) 当我第一次导入.sql文件时报错: mysql> source ...