未验证 提交 1fbb0330 编写于 作者: B BruceCat 提交者: GitHub

【100.相同的树】【C++】

【100.相同的树】【C++】
......@@ -310,7 +310,8 @@ void BST(TreeNode root, int target) {
<img src="../pictures/qrcode.jpg" width=200 >
</p>
======其他语言代码======
======其他语言代码======
### c++
......@@ -348,6 +349,43 @@ public:
};
```
[yanggg1997](https://github.com/yanggg1997)提供第100题C++代码:
``` c++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
// 若当前节点均为空,则此处相同
if(!p && !q) return true;
// 若当前节点在一棵树上有而另一棵树上为空,则两棵树不同
if(!p && q) return false;
if(p && !q) return false;
// 若当前节点在两棵树上均存在。
if(p->val != q->val)
{
return false;
}
else
{
// 向左右子树分别递归判断
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
}
};
```
### python
[ChenjieXu](https://github.com/ChenjieXu)提供第98题Python3代码:
......@@ -400,6 +438,7 @@ class Solution:
return p.val==q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
```
[Edwenc](https://github.com/Edwenc) 提供 leetcode第450题的python3 代码:
```python
......@@ -448,4 +487,4 @@ class Solution:
while node.right:
node = node.right
return node
```
\ No newline at end of file
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册