Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
42fb596
+linked-list.md +reverse-linked-list
Igneaalis May 13, 2020
080e833
Changed variables' name, added hyperlinks (anchors)
Igneaalis May 22, 2020
0d5a662
Improved [Reverse linked list]
Igneaalis May 22, 2020
57b1910
Added LEETCODE middle-of-the-linked-list solution.
Igneaalis Jun 1, 2020
303c7ae
Added LEETCODE palindrome-linked-list solution.
Igneaalis Jun 1, 2020
cbc585f
Added LEETCODE merge-two-sorted-lists solution.
Igneaalis Jun 1, 2020
07c4497
Added LEETCODE Merge Two Sorted Lists (iterative) solution.
Igneaalis Jun 1, 2020
5d394cc
Added a forgotten link for an iterative solution.
Igneaalis Jun 1, 2020
f6c5605
Added LEETCODE "binary-tree-inorder-traversal" solution.
Igneaalis Jun 1, 2020
4101f7e
Added recursive and iterative,stack LEETCODE "binary-tree-inorder-tra…
Igneaalis Jun 2, 2020
164fd7e
Removed an extra line from recursive solution.
Igneaalis Jun 2, 2020
bee6f14
Removed an extra line from recursive solution (another).
Igneaalis Jun 2, 2020
07205fc
Added LEETCODE "symmetric-tree" solution with euler path.
Igneaalis Jun 3, 2020
0756cfd
Added LEETCODE "maximum-depth-of-binary-tree" solution.
Igneaalis Jun 3, 2020
9b155a3
Added LEETCODE "same-tree" recursive solution.
Igneaalis Jun 3, 2020
9f4cb34
Added LEETCODE "maximum-depth-of-binary-tree" recursive solution.
Igneaalis Jun 3, 2020
e732f70
Added LEETCODE "same-tree" recursive solution.
Igneaalis Jun 3, 2020
c6f6e52
Added LEETCODE "invert-binary-tree" recursive solution.
Igneaalis Jun 3, 2020
c313d6f
Added LEETCODE "path-sum" recursive solution.
Igneaalis Jun 3, 2020
06a7d96
Added LEETCODE "binary-tree-level-order-traversal" recursive solution.
Igneaalis Jun 3, 2020
3a1f0bd
Added LEETCODE "subtree-of-another-tree" recursive solution.
Igneaalis Jun 3, 2020
d0be9f6
Added LEETCODE "kth-smallest-element-in-a-bst" recursive solution.
Igneaalis Jun 4, 2020
a7058f5
Added LEETCODE "validate-binary-search-tree" recursive solution.
Igneaalis Jun 4, 2020
01d41ae
Added LEETCODE "validate-binary-search-tree" recursive solution.
Igneaalis Jun 5, 2020
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.

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

+ [Validate Binary Search Tree (pythonic way)](#validate-binary-search-tree-pythonic-way)
+ [Validate Binary Search Tree (recursive)](#validate-binary-search-tree-recursive)

## Validate Binary Search Tree (pythonic way)

https://leetcode.com/problems/validate-binary-search-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 isValidBST(self, _root, first=True):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Надо написать решение похожее на postOrder, когда ты передаешь нужные границы в левый рекурсивный вызов и в правый рекурсивный вызов. Далее тебе возвращаются булевы значения, что в поддеревьях все хорошо или нет

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
:type first: bool
:rtype: bool
"""
root = _root
if not root:
return first or []
result = self.isValidBST(root.left, False) + [root.val] + self.isValidBST(root.right, False)
return all([right > left for right, left in zip(result[1:], result)]) if first else result

```

## Validate Binary Search Tree (recursive)

https://leetcode.com/problems/validate-binary-search-tree/

```python
# Definition for a binary tree root.
# 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 check(self, root, left=None, right=None):
"""
:type root: TreeNode
:type left: TreeNode
:type right: TreeNode
:rtype: bool
"""
if not root:
return True
if left is not None and root.val <= left:
return False
if right is not None and root.val >= right:
return False
return self.check(root.left, left, root.val) and self.check(root.right, root.val, right)

def isValidBST(self, _root):
"""
:type _root: TreeNode
:rtype: bool
"""
root = _root
return self.check(root)

```