(leetcode) Same Tree
2014-09-18 15:47
309 查看
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.
解题思路: 递归判断二叉树 根结点val是否相同,然后判断结构是否一样,调用递归。
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
解题思路: 递归判断二叉树 根结点val是否相同,然后判断结构是否一样,调用递归。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { bool function(TreeNode *p, TreeNode *q){ if(p==NULL&&q==NULL) return true; if(p==NULL&&q!=NULL||p!=NULL&q==NULL||p->left!=NULL&&q->left==NULL||p->left==NULL&&q->left!=NULL||p->right==NULL&&q->right!=NULL||p->right!=NULL&&q->right==NULL) return false; return (p->val==q->val)&&(function(p->left,q->left))&&(function(p->right,q->right)); } public: bool isSameTree(TreeNode *p, TreeNode *q) { return function(p,q); } };
相关文章推荐
- leetcode Same Tree
- LeetCode之Same Tree
- Leetcode: Same Tree 理解分析
- Leetcode Same Tree
- [LeetCode]: 100: Same Tree
- LeetCode Same Tree
- leetcode: (100) Same Tree
- Leetcode Same Tree 判断两棵二叉树是否相同
- <LeetCode OJ> Same Tree【100】
- leetcode — same-tree
- 关于Leetcode中Same Tree一题的理解
- Leetcode: Same Tree
- LeetCode-Easy刷题(19) Same Tree
- LeetCode Same Tree
- LeetCode Same Tree
- LeetCode之Same Tree
- LeetCode Same Tree (判断相同树)
- leetcode 100 SameTree 3行
- LeetCode Same Tree
- leetcode Same Tree python