Skip to content

feat(ml):$101.symmetric-tree.md #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions problems/101.symmetric-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,84 @@ return True
> 其实更像本题一点的话应该是从中间分别向两边扩展 😂

## 代码
代码支持:C++, Java, Python3

C++ Code:
```c++
/**
* 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 isSymmetric(TreeNode* root) {
return root==NULL?true:recur(root->left, root->right);
}

bool recur(TreeNode* l, TreeNode* r)
{
if(l == NULL && r==NULL)
{
return true;
}
// 只存在一个子节点 或者左右不相等
if(l==NULL || r==NULL || l->val != r->val)
{
return false;
}

return recur(l->left, r->right) && recur(l->right, r->left);
}
};
```


Java Code:
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null)
{
return true;
}
else{
return recur(root.left, root.right);
}
// return root == null ? true : recur(root.left, root.right);
}

public boolean recur(TreeNode l, TreeNode r)
{
if(l == null && r==null)
{
return true;
}
// 只存在一个子节点 或者左右不相等
if(l==null || r==null || l.val != r.val)
{
return false;
}

return recur(l.left, r.right) && recur(l.right, r.left);
}
}
```

Python3 Code:
```py

class Solution:
Expand Down