Leetcode在线编程same-tree
2016-12-04 22:10
281 查看
Leetcode在线编程same-tree
题目链接
Leetcode在线编程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.
题意
判断题目给定的2个二叉树是否相等解题思路
先判断是否2个可能不能同时为空或者同时为非空(异或大法)接着若有一个为空,则肯定两个都是空了
最后就是递归判断左右子树是否都是相等
AC代码
class Solution { public: bool isSameTree(TreeNode *p, TreeNode *q) { if(!p ^ !q) return false; if(!p) return true; if(p->val == q->val) return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); else return false; } };
相关文章推荐
- leetcode在线编程binary-tree-postorder-traversal
- Leetcode在线编程 binary-tree-inorder-traversal
- leetcode:100 Same Tree-每日编程第六题
- Leetcode在线编程minimum-depth-of-binary-tree
- LeetCode Same Tree
- LeetCode之Same Tree
- [LeetCode]Same Tree解题报告
- Leetcode Same Tree
- Leetcode::SameTree
- LeetCode Same Tree
- LeetCode Same Tree
- leetcode: Same Tree
- Leetcode Same Tree
- LeetCode Same Tree
- LeetCode: Same Tree [100]
- Leetcode: Same Tree 理解分析
- [leetcode.com]算法题目 - Same Tree
- leetcode 65: Same Tree
- LeetCode Same Tree 决定是否是相同的树
- [leetcode]_Same Tree