Same Tree
来源:互联网 发布:项目数据分析师 上海 编辑:程序博客网 时间:2024/06/06 15:04
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.
Have you been asked this question in an interview?
主要要考虑的corner case : 一个为null, 另外一个不为
/** * Definition for binary tree * 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) ||(p != null && q == null)) { return false; } if (p.val == q.val) { return (isSameTree(p.left, q.left) && (isSameTree(p.right, q.right))); } else { return false; } }}
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
- 理解$watch ,$apply 和 $digest --- 理解数据绑定过程
- 15.8 Pascal's Triangle
- USACO Letter Game 解题报告
- 部署中遇到的问题(四)
- Lucene 4利用Codec实现压缩存储
- Same Tree
- 【c++应用程序设计】第2章 控制流
- window批处理-5 start
- Python 访问Facebook (http)
- J2SE基础:10集合2
- 将C语言的字符串转为OC的字符串
- miRNA研究在线数据库
- 今日作息及食谱(8.7)
- geeksforgeeks Count number of binary strings without consecutive 1′s