Leetcode 100. Same Tree 验证树是否相同 解题报告
2016-09-04 15:41
627 查看
1 解题思想
这道题就是说给了两个二叉树,判断他们是否一样(就是树的结构和值)这题是在很简单,直接按照相同的方式遍历一下就好,随便你用什么顺序
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.
3 AC解
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ //以同样的方式遍历一下就好了啊 public class Solution { 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); } }
相关文章推荐
- AVL树-自平衡二叉查找树(Java实现)
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- 关于C#中排序函数的总结
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua 学习笔记之C API 遍历 Table实现代码
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法