Same Tree
来源:互联网 发布:美丽折淘客软件下载 编辑:程序博客网 时间:2024/06/15 18:11
https://oj.leetcode.com/problems/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.
public boolean isSameTree(TreeNode p, TreeNode q)
这一题难度是easy,那么我也就只是简单的说一下吧。利用DFS使得两棵树走同样的路径。看看能不能一直匹配完所有节点。这一题BFS较难,因为你每一层相同都没办法保证你的结构是一样的,需要另外花大力气去维护结构上的一致,没必要。
代码如下:
public boolean isSameTree(TreeNode p, TreeNode q) { if(p == null && q == null) return true; if(p == null || q == null) return false; if(p.val != q.val) return false; return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); }
0 0
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Spring prototype bean 怎么了
- CUDA Review
- C#之浅析面向对象编程思想(一)
- ACM #1015 KMP算法
- 【iOS开发-119】ipa打包、单元测试test、本地通知UILocalNotification
- Same Tree
- 为什么openstack中的oslo模块总喜欢发生代码冲突? (by quqi99)
- 一个简单的C共享库的创建及python调用此库的方法
- CPU及Linux内核中不同地址概念
- Java虚拟机运行时数据区域
- crontab中执行其他账号运行的程序 免密码
- cocos2dx2.0 ----- cocos2dx3.0
- 常见错误总结
- DLNA简介