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.

98 changes: 98 additions & 0 deletions tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Binary tree

+ [Binary Tree Inorder Traversal (iterative, threaded)](#binary-tree-inorder-traversal-iterative-threaded)
+ [Binary Tree Inorder Traversal (recursive)](#binary-tree-inorder-traversal-recursive)
+ [Binary Tree Inorder Traversal (iterative, stack)](#binary-tree-inorder-traversal-iterative-stack)

## Binary Tree Inorder Traversal (iterative, threaded)

https://leetcode.com/problems/binary-tree-inorder-traversal/

```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 inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
head = root
inorderlist = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Очень круто, но много кода. Ты такой код не успеешь написать быстро, много разных if. Можешь оставить

Но напиши рядом рекурсивное решение и решение, где ты рекурсию раскроешь через stack, т.е. получится итеративное решение без лишних if

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Наличие многих if - место для потенциальной ошибки. Решения здесь проще

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.

@Vivelapaix добавил два решения. Ещё вопрос по гиту. Как изменить имя запушенного коммита?

while head:
if not head.left:
inorderlist.append(head.val)
head = head.right
elif head.left:
curNode = head.left
while head.left and curNode.right and curNode.right != head:
curNode = curNode.right
if not curNode.right:
curNode.right = head
head = head.left
elif curNode.right == head:
inorderlist.append(head.val)
curNode.right = None
head = head.right
return inorderlist

```

## Binary Tree Inorder Traversal (recursive)

https://leetcode.com/problems/binary-tree-inorder-traversal/

```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 inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
head = root
return self.inorderTraversal(head.left) + [head.val] + self.inorderTraversal(head.right) if head else []

```

## Binary Tree Inorder Traversal (iterative, stack)

https://leetcode.com/problems/binary-tree-inorder-traversal/

```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 inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if not root:
return []
stack = []
inorderlist = []
while stack or root:
if root:
stack.append(root)
root = root.left
else:
node = stack.pop()
inorderlist.append(node.val)
root = node.right
return inorderlist

```