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.
检查两颗二叉树是否一样,基本上都是一个套路,这里稍微不一样的就是递归到每一个节点都需要判断是否一样。
有一个节点不一样就可以判断不是一样的二叉树了。如果递归完了还是一样,那么就是一样的两颗二叉树了。这也是为什么判断变量isSame初始化为true了。
/**
* Definition for binary tree
* 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)
{
bool isSame = true;
HelperSame(p, q, isSame);
return isSame;
}
void HelperSame(TreeNode *p, TreeNode *q, bool &isSame)
{
if(!p && !q) return;
if (!p&&q || p&&!q)
{
isSame = false;
return;
}
if (q->val != p->val)
{
isSame = false;
return;
}
if (p && q)
HelperSame(p->left, q->left, isSame);
if (p && q)
HelperSame(p->right, q->right, isSame);
}
};
//2014-2-15 update
bool isSameTree(TreeNode *p, TreeNode *q)
{
if (!p && !q) return true;
if (!p && q || p && !q || p->val != q->val) return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
分享到:
相关推荐
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
leetcode 答案leetcode-java leetcode.com 的 Java 答案 ================索引================ com.leetcode.array Search a 2D Matrix Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II ...
Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...
leetcode刷多少面试 LeetCode 划分说明 这个重点题目是把Leetcode前400题进行精简,划分精简规则如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目...
leetcode 答案 leetcode 08/18 Unique Paths 应该是简单的数学排列组合问题,提炼一下其实就一句话:有m个黑球,n个白球,有多少种不同的排列方式。 我数学太差,没找到答案,直接上了动态规划。 Unique Paths II ...
leetcode-js Leecode 经典题目 JavaScript TypeScript 题解...100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 (Pascal's Triangle) 119.杨辉三角 II (Pascal's Triangle)
[100_same-tree.cpp] [1001_sorted-merge-lcci.cpp] [101_对称树.cpp] [102_binary-tree-level-order-traversal.cpp] [103_binary-tree-zigzag-level-order-traversal.cpp] [104_maximum-depth-of-binary-tree.cpp] ...
与大家分享我学习算法的...数组/链表:树相关:AVLTree 平衡二叉搜索树BinaryHeap 二叉堆(优先队列)Num2TreeSum 数组树的和MaxDepth4Tree (leetcode 104)ValidBinarySearchTree (leetcode 98)SameTree (leetcode 100)...
leetcode 2 和 c Leetcode_questions 目前拥有: 简单的: 1.二和(c) 7.反转整数(c) 9.回文数(c) 14.最长公共前缀(python) 20.有效括号(python) 21.合并两个排序列表(c) 26.从排序数组中删除重复项 (c) 27.删除...
sameTree: find_content_children: LeetCode 算法题 时间复杂度和空间复杂度权衡,时间复杂度的提升是以空间复杂度为代价的 仔细观察,LeetCode 上对每一次代码的提交的 执行时间 && 消耗内存 效率 = 算法效率 + ...
leetcode叫数 Leetcode_JS leetcode编程题,javascript版本 ##NO.35 Search Insert Position 这道题非常简单,数组是有序数组,只需要遍历一遍数组,判断当前值是否等于target或者大于target即可返回其位置值。如果...
同一棵树:#[./LeetCode/100_Same Tree.cpp] 序列化和反序列化二叉树:# 将根数与叶数相加:# 二叉搜索树 最低共同祖先:1); 2) 红黑树 AVL树 展开树 段树RMQ:范围最小查询 DFS:由,由 (单一来源) (所有对) ...
leetcode分发糖果 Leetcode C++ Solution Don't try to understand it, feel ...21-合并两个有序链表:merge-two-sorted-lists 83-删除排序链表中的重复元素:...100-相同的二叉树:same-tree 101-对称二叉树:symmetric-
https://leetcode.com/problems/same-tree/ Same Tree 101 https://leetcode.com/problems/symmetric-tree/ Symmetric Tree 102 https://leetcode.com/problems/binary-tree-level-order-traversal/ Binary Tree ...
Same Tree Symmetric Tree Balanced Binary Tree Flatten Binary Tree to Linked List Populating Next Right Pointers in Each Node II 二叉树的构建 Construct Binary Tree from Preorder and Inorder Traversal ...