Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 0 additions & 3 deletions README.md

This file was deleted.

78 changes: 78 additions & 0 deletions tree.md
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):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 не пройдет

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Щас поправлю.

"""
:type root: TreeNode
:rtype: bool
"""
path = []

def euler(node):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Что за euler ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Напиши рядом рекурсивное решение

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

euler - реализация Эйлерова путя. Рекурсивное добавлю.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The 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)

```