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.

Hide Tags

Tree Depth-first Search

 

class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if(p==NULL&&q==NULL) return true;
if(p==NULL||q==NULL||p->val!=q->val) return false;
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
}
};

[LeetCode] Same Tree 深度搜索的更多相关文章

  1. [LeetCode] Balanced Binary Tree 深度搜索

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  2. [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  3. [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. [LeetCode] Populating Next Right Pointers in Each Node 深度搜索

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  5. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  6. [LeetCode] Path Sum II 深度搜索

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  7. Leetcode 课程表 C++ 图的深度搜索和广度搜索练习

    广度搜索(degree) struct GraphNode{ int label; vector<GraphNode*> neighbours; GraphNode(int x):labe ...

  8. leetcode—Same Tree

    1.题目描述 Given two binary trees, write a function to check if they are equal or not.   Two binary tree ...

  9. LeetCode:二叉搜索树中第K小的数【230】

    LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ...

随机推荐

  1. mysql update 多表关联更新

    UPDATE new_schedules_spider_static_schedule s join new_scac_port p on p.`PORT` = s.`PORT` and p.SCAC ...

  2. Cisco交换机与路由器命令总结

    1.查看信息 show version    查看版本及引导信息 show running-config     查看运行设置 show startup-config     查看开机设置 show ...

  3. JZOJ 3385. 【NOIP2013模拟】黑魔法师之门

    3385. [NOIP2013模拟]黑魔法师之门 (Standard IO) Time Limits: 1000 ms  Memory Limits: 131072 KB  Detailed Limi ...

  4. psutil——获取系统信息的Python第三方模块

    本文摘自廖雪峰大神个人网站:https://www.liaoxuefeng.com/wiki/1016959663602400/1183565811281984 用Python来编写脚本简化日常的运维 ...

  5. django开发基础

    一.配置静态文件 https://www.cnblogs.com/lshedward/p/10351051.html 二.路由分发 https://www.cnblogs.com/lshedward/ ...

  6. I miss you, Jenny【我想念你,jenny】

    I miss you, Jenny Forrest Gump: 阿甘正传 You died on a Saturday morning. And I had you placed here our t ...

  7. windows server 2008解决无法PING通问题

    今天安装服务器(server 2008),配置完IP地址后,发现局域网其它电脑无法PING通服务器,测线仪测试链路都正常,网线接别的电脑也正常,以为是网卡问题,于是ping了自己的IP,发现能PING ...

  8. P1198 [JSOI2008]最大数(单调栈)

    P1198 [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制: ...

  9. 4444: [Scoi2015]国旗计划

    4444: [Scoi2015]国旗计划 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 485  Solved: 232 Description A国 ...

  10. python 二(续)——面向对象编程进阶

    1.类的成员 2.类成员修饰符 3.类的特殊成员 在python第二课——面向对象初级,文章中介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一 ...