[LeetCode]100. Same Tree--判断二叉树是否相同
2017-08-03 09:14
417 查看
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 == NULL && q == NULL) return true; else if(p == NULL || q == NULL) return false; else 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 98 Validate Binary Search Tree判断是否为合法二叉树
- Leetcode 100. Same Tree 验证树是否相同 解题报告
- LeetCode(Balanced Binary Tree ) 判断一个二叉树是否为平衡二叉树
- LeetCode刷题之三:判断两个二叉树是否相同
- LeetCode(Validate Binary Search Tree) 判断一个二叉树是否是二叉搜索树
- 【leetcode】比较两棵二叉树是否相同(Same Tree)
- leetcode_101. Symmetric Tree 对称树, 判断一棵二叉树是否对称,递归方法
- Leetcode:Validate Binary Tree 判断一个二叉树是否为二叉搜索树
- LeetCode基础--二叉树-判断两个树是否相同
- leetcode_110. Balanced Binary Tree 判断一棵二叉树是否为平衡二叉树
- 每天一道LeetCode-----判断两个二叉树是否相同
- 判断给定二叉树是否是二叉搜索树(LeetCode: Validate Binary Search Tree)
- 【遍历二叉树】08判断两个二叉树是否相同【Same Tree】
- 判断两棵二叉树是否结构相同
- LeetCode Same Tree 决定是否是相同的树
- LeetCode 100. Same Tree(二叉树)
- 100. Same Tree 树是否相同
- Leetcode刷题记——100. Same Tree(相同的树)