Leetcode Same Tree
2015-05-18 19:40
253 查看
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.
用递归的思想做
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
用递归的思想做
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(!p&&!q) return true; if (!p||!q) { return false; } if(p&&q) { if (p->val != q->val) { return false; } else { return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right); } } } };
相关文章推荐
- 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-100: Same Tree
- [LeetCode] [Same Tree 2012-09-03 ]
- LeetCode:Same Tree
- LeetCode -- Same Tree
- 4.5 leetcode -5 same-tree
- LeetCode:Same Tree - 判断两颗树是否相等
- [Leetcode]-Same Tree
- LeetCode:Same Tree
- [LeetCode] Same Tree
- leetcode Same Tree
- 100. Same Tree Leetcode Python
- Leetcode Same Tree