Leetcode-100: Same Tree

Drools vs URule:5个真实项目场景告诉你如何选择开源规则引擎 本文通过电商风控、物流计费、会员体系、保险定价和物联网告警五个真实项目场景,深度对比了Drools与URule两款开源规则引擎。分析了两者在规则复杂度、性能表现、开发运维及适用场景上的核心差异,为技术选型提供基于实战的决策指南。 阅读详情

这题解法很多,我就用的最简单的递归和非递归前序遍历。
解法1:递归

#include <iostream>
#include <stack>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

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

}

int main()
{
    TreeNode a1(1), b1(2), c1(1);
    TreeNode a2(1), b2(2), c2(1);
    a1.left=&b1;
    a1.right=&c1;
    a2.left=&b2;
    a2.right=&c2;

    cout<<isSameTree(&a1, &a2);

    return 0;
}

解法2:前序遍历。

//preorder
bool isSameTree(TreeNode* p, TreeNode* q) {
    stack<TreeNode*> s1, s2;
    if ((p&&!q) || (!p&&q)) return false;
    while((p || !s1.empty()) && (q || !s2.empty())) {
      while(p && q) {
            if (p->val==q->val) {
                s1.push(p);
                p=p->left;
                s2.push(q);
                q=q->left;
            } else {
                return false;
            }
        }

        if ((p && !q) || (!p && q)) return false;
        if ((s1.empty() && !s2.empty()) || (!s1.empty() && s2.empty())) return false;

        if (!s1.empty() && !s2.empty()) {
            p=s1.top();
            q=s2.top();
            s1.pop();
            s2.pop();
            p=p->right;
            q=q->right;
        }

    }

    return true;
}

以后再做
解法3:非递归中序遍历
解法4:非递归后序遍历
解法5:按层次遍历(用队列)

MATLAB基础应用精讲-【图像处理】GNN图神经网络(附python代码实现) 图神经网络由于其在处理非欧空间数据和复杂特征方面的优势,受到广泛关注并应用于推荐系统、知识图谱、交通道路分析等场景。图结构可描述复杂关系,如基因结构、通信网络、交通路线、社交网络等。图计算挖掘结构信息,但无法学习节点特征。神经网络在欧氏空间数据上表现优越,但无法直接应用于非欧氏空间的图数据。图神经网络结合图计算和神经网络优势,处理非欧氏空间数据及其复杂特征,应用于网络链接预测、推荐系统和交通道路分析等场景。实际应用中,图数据规模庞大,如百亿级节点、千亿级边,存储开销超过10TB。 阅读详情

相关推荐

HDLbits练习11 - Counter 1-12 ( Exam/ece241 2014 q7a )

(如这题用于当复位信号为高或者计数器递增至12时,该信号置1使计数器的输入信号 ’ d ’ 变为 ’ 4’b0001 ’ ,再加载到 ’ Q ’ 中。当 ’ load ’ 信号为高时,在时钟的上升沿,计数器会将 ’ d ’ 输入的值加载到 ’ Q ’ (计数器的输出)中,而不是递增当前值。用途:在时钟上升沿触发的情况下,如果 ’ enable ’ 信号为高,计数器的值会递增,如果 ’ load ’ 信号为高,计数器将加载 ’ d ’ 信号的值。作用: ’ Q ’ 是计数器的当前值,也是模块的主要输出。

weixin_62830176的博客 474

Leetcode 100 : same Tree

Leetcode—100same TreeGiven 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 valu

Code_J_xer的博客 418

SECS/GSM 测试工具 SecsExpress 的配置与实战应用

本文详细介绍了半导体设备通讯测试工具SecsExpress的配置与实战应用。作为专业的SECS/GSM协议调试工具,它能模拟Host与设备端进行双向测试,帮助工程师快速部署、验证通讯链路,并高效排查与RMS系统或真实设备间的联调问题,是提升半导体工厂设备集成与维护效率的利器。

yhn456789的博客 312

LeetCode: Same Tree [100]

【题目】 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. 【题意】 判断了两个二叉树是否相等 【思路】 递归

Harry Huang 922

RK3568/RK3588之修改8250驱动实现RS485收发的自动切换

在串口发送前,若为485,则将控制引脚拉为发送状态,在发送后,循环查看是否发送完成,若发送完成则将引脚置回读状态,如此一来,就能实现在内核测控制方向切换,而不会因为延时造成数据丢失,经过测试此办法可行。全部修改完成后,重新编译生成内核文件和设备树文件,烧写到板子上即可。

哒宰的博客 3130

RJ45以太网接口EMC设计方案

RJ45以太网接口EMC设计方案 一、 接口概述 RJ45以太网接口是目前应用最广泛的通讯设备接口,以太网口的电磁兼容性能关系到通讯设备的稳定运行。赛盛技术应用电磁兼容设计平台(EDP)软件从接口原理图、结构设计,线缆设计三个方面来设计以太网口的EMC设计方案。 二、 接口电路 原理图的EMC设计 百兆以太网接口2KV防雷滤波设计 图1 百兆以太网接口2KV防雷滤波设计 接口电路设计概述: 本方案从EMC原理上,进行了相关的抑制干扰和抗敏感度的设计;从设计层次解决EMC问题;同时此电路兼容了百兆以太网接

兰陵笑笑生SD的博客 1万+

LeetCode: 100. Same Tree

LeetCode: 100. Same Tree 题目描述 Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the node...

杨领well的专栏 194

Unity安卓打包实战:SDK与JDK配置全攻略

本文详细介绍了Unity安卓打包过程中SDK与JDK的配置方法,包括工具下载、安装步骤、环境变量设置及常见错误排查。通过实战指南帮助开发者快速完成Unity安卓打包环境搭建,提升开发效率。

weixin_30338481的博客 427

LeetCode 100: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. 给定两棵二叉树,编写

judadeshu的博客 284

泛微ecology9 HRMwebservice接口实战:企业组织架构与人员信息同步指南

本文详细介绍了泛微ecology9 HRMwebservice接口在企业系统集成中的实战应用,重点解析了如何通过该接口实现组织架构与人员信息的双向同步。内容涵盖环境配置、核心接口调用、数据格式规范及常见错误排查,旨在帮助开发与运维工程师高效完成主数据同步,提升企业IT管理效率。

weixin_29323365的博客 511

【乐企】开票控制服务

例子:企业信息初始化。

九七的博客 466

Leetcode 100: Same Tree

Same Tree Easy 1262 43 Favorite Share Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and t...

qq_34314736的博客 168

vue3+ts+elementui-plus二次封装树形表格实现不同层级展开收起的功能

我只定义了三层数据,就只演示展开了三层,还可以多层,自己设置即可。

蜗牛~~ 2609

leetcode记录:问题简单:100. Same Tree

题目出处:https://leetcode.com/problems/same-tree/ 题目描述: 给定两个二叉树,编写一个函数来检查它们是否相同。 如果两个二叉树在结构上相同并且节点具有相同的值,则认为它们是相同的。 Example 1: Input: 1 1 1 1 ...

unAlpaca的博客 215

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Leetcode 100: Same Tree

题目: python3代码: # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isSameTree(self, p: Tree

CHNMSCS 268

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode Q100 : 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. /** * Def

qwe_lingkun的专栏 463

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

leetcode[100]:Same Tree

Same TreeGiven 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./** * Def

hahubaba的博客 241

LeetCode 100: Same Tree

Same TreeGiven 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.解题思路递归(rec

一只码畜 279

LeetCode笔记:100. Same Tree

比较两个二叉树是否相等

Minecraft 1371

LeetCode 100: 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. 分析: 题目要求判

哈哈的个人专栏 850

LeetCode100:Same Tree

Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the nodes have the same value. Example ...

卷卷萌的博客 221

LeetCode 100: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. /** * Def

henryzhihua 719

leetcode(100):Same Tree

题目:Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the nodes have the same value. 题目分析:...

赵小越的博客 222

leetcode: 100. Same Tree

QGiven two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Example 1:In

JNing 613

Leetcode : 100 Same Tree

题目如下:100. Same TreeGiven two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same...

qq_41794348的博客 161
上一篇: Leetcode-82: Remove Duplicates from Sorted List II
下一篇: Leetcode-48: Rotate Image
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址