[LeetCode]Same Tree
来源:互联网 发布:javascript 执行顺序 编辑:程序博客网 时间:2024/06/07 02:46
Question
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.
本题难度Easy。
递归
【复杂度】
时间 O(N) 空间 O(1)
【思路】
递归对比两个节点的值。
【代码】
/** * 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) { //base case if(p==q&&p==null) return true; return p!=null&&q!=null&&p.val==q.val &&isSameTree(p.left,q.left) &&isSameTree(p.right,q.right); }}
0 0
- Leetcode - Tree - Same Tree
- [leetcode][tree] Same Tree
- LeetCode: Same Tree
- LeetCode Same Tree
- [Leetcode] Same Tree
- LeetCode : Same Tree
- [LeetCode] Same Tree
- leetcode 65: Same Tree
- Leetcode 100 Same Tree
- [leetcode] Same Tree
- [LeetCode]Same Tree
- [leetcode]Same Tree
- [Leetcode]Same Tree
- Leetcode: Same Tree
- [LeetCode] Same Tree
- LeetCode-Same Tree
- [leetcode] Same Tree
- LeetCode - Same Tree
- Naive Bayes、KNN、Softmax分类器的比较研究(附代码地址)
- 大数据征信如何为一个人建立数据肖像?
- arm linux 调试记录
- H5选择器
- 【.Net码农】【PropertyInfo】C# list导出Excel
- [LeetCode]Same Tree
- AOJ.592 神奇的叶子
- Linux开发工具之工程管理器make
- B-树的操作总结
- iOS10 权限问题
- UVA 12563 Jin Ge Jin Qu hao 在有约束条件下的DP,结构体作为基本单位
- 【NOI导刊】【归并排序求逆序对】最接近神的人
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions//734C
- 捋一捋 Activity 的启动流程 以及 界面的初始化过程