LeetCode刷题(C++)——Same Tree(Easy)
来源:互联网 发布:小黑屋码字软件下载 编辑:程序博客网 时间:2024/05/21 01:58
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.
思路:递归,判断根结点是否相同,不同返回false,相同的话再递归判断左子树和右子树
/** * 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==0){ return q==0; } if(q==0){ return false; } return (p->val==q->val) && isSameTree(p->left,q->left) && isSameTree(p->right,q->right); }};
阅读全文
0 0
- LeetCode刷题(C++)——Same Tree(Easy)
- LeetCode刷题(C++)——Symmetric Tree(Easy)
- LeetCode-Easy刷题(19) Same Tree
- leetcode 100 Same Tree(难易度:Easy)
- LeetCode 100. Same Tree 题解(C++)
- [Leetcode 100, easy] Same Tree
- 【LeetCode】(100)Same Tree(Easy)
- <LeetCode><Easy> 100 Same Tree
- LeetCode #100 - Same Tree - Easy
- 【leetcode】100. Same Tree(easy)
- LeetCode刷题(C++)——Minimum Depth if Binary Tree(Easy)
- LeetCode刷题(C++)——Maximum Depth of Binary Tree(Easy)
- LeetCode刷题(C++)——Binary Tree Level Order Traversal II(Easy)
- LeetCode刷题(C++)——Convert Sorted Array to Binary Search Tree(Easy)
- LeetCode刷题(C++)——Balanced Binary Tree(Easy)
- Easy 20 Same Tree(100)
- leetCode刷题——Same Tree
- leetcode: Same Tree (C++)
- 路径之谜
- chrome抓包方法
- SEM_UNDO
- 安装VSCODE运行C++
- 游戏心得
- LeetCode刷题(C++)——Same Tree(Easy)
- BZOJ 2456 mode(找众数)
- spring中定时器的使用
- Oracle 12C 新特性之 恢复表
- 被误解的MVC和被神化的MVVM
- 省时的浏览器同步测试工具 browersync
- 符号匹配
- 安卓的styles.xml文件和theme的使用
- 进程保活之双进程守护