博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode - Same Tree
阅读量:4696 次
发布时间:2019-06-09

本文共 626 字,大约阅读时间需要 2 分钟。

题目:

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.

思路:

递归

package tree;public class SameTree {    public boolean isSameTree(TreeNode p, TreeNode q) {        if (p == null && q == null) return true;        if (p == null || q == null) return false;        return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);    }        public static void main(String[] args) {        // TODO Auto-generated method stub    }}

 

转载于:https://www.cnblogs.com/null00/p/5117804.html

你可能感兴趣的文章
Java环境变量设置
查看>>
【JBPM4】判断节点decision 方法3 handler
查看>>
filter 过滤器(监听)
查看>>
node启动时, listen EADDRINUSE 报错;
查看>>
杭电3466————DP之01背包(对状态转移方程的更新理解)
查看>>
kafka中的消费组
查看>>
python--注释
查看>>
SQL case when else
查看>>
SYS_CONTEXT 详细用法
查看>>
Pycharm配置autopep8让Python代码更符合pep8规范
查看>>
我的第一篇博客
查看>>
【C++算法与数据结构学习笔记------单链表实现多项式】
查看>>
C#垃圾回收机制
查看>>
31、任务三十一——表单联动
查看>>
python之hasattr、getattr和setattr函数
查看>>
maven使用阿里镜像配置文件
查看>>
Copy code from eclipse to word, save syntax.
查看>>
arguments.callee的作用及替换方案
查看>>
23 Java学习之RandomAccessFile
查看>>
P2709 小B的询问
查看>>