-
Notifications
You must be signed in to change notification settings - Fork 0
Leetcode symmetric tree #16
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
base: master
Are you sure you want to change the base?
Changes from all commits
42fb596
080e833
0d5a662
57b1910
303c7ae
cbc585f
07c4497
5d394cc
f6c5605
4101f7e
164fd7e
bee6f14
07205fc
4c39594
29f6ff2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Binary tree | ||
|
|
||
| + [Symmetric Tree (euler path)](#symmetric-tree-euler-path) | ||
| + [Symmetric Tree (recursive)](#symmetric-tree-recursive) | ||
|
|
||
| ## Symmetric Tree (euler path) | ||
|
|
||
| https://leetcode.com/problems/symmetric-tree/ | ||
|
|
||
| ```python | ||
| # Definition for a binary tree node. | ||
| # class TreeNode(object): | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution(object): | ||
| def isSymmetric(self, root): | ||
| """ | ||
| :type root: TreeNode | ||
| :rtype: bool | ||
| """ | ||
| path = [] | ||
|
|
||
| def euler(node): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Что за euler ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Напиши рядом рекурсивное решение
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. euler - реализация Эйлерова путя. Рекурсивное добавлю.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Добавил рекурсивное решение. |
||
| if not node: | ||
| path.append(None) | ||
| return | ||
|
|
||
| path.append(node.val) | ||
| euler(node.left) | ||
| path.append(node.val) | ||
| euler(node.right) | ||
| path.append(node.val) | ||
|
|
||
| euler(root) | ||
| return path == path[::-1] | ||
|
|
||
| ``` | ||
|
|
||
| ## Symmetric Tree (recursive) | ||
|
|
||
| https://leetcode.com/problems/symmetric-tree/ | ||
|
|
||
| ```python | ||
| # Definition for a binary tree node. | ||
| # class TreeNode(object): | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def isSymmetric(self, root): | ||
| """ | ||
| :type root: TreeNode | ||
| :rtype: bool | ||
| """ | ||
| if not root: | ||
| return True | ||
|
|
||
| def isSymmetricBst(nodeA, nodeB): | ||
| """ | ||
| :type nodeA: TreeNode | ||
| :type nodeB: TreeNode | ||
| :rtype: bool | ||
| """ | ||
| if not nodeA and not nodeB: | ||
| return True | ||
| elif not (nodeA and nodeB): | ||
| return False | ||
| else: | ||
| return nodeA.val == nodeB.val and isSymmetricBst(nodeA.left, nodeB.right) and isSymmetricBst( | ||
| nodeA.right, | ||
| nodeB.left) | ||
|
|
||
| return isSymmetricBst(root.left, root.right) | ||
|
|
||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP8 не пройдет
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Щас поправлю.