Skip to content

Commit

Permalink
tree
Browse files Browse the repository at this point in the history
  • Loading branch information
PingZ7 committed Feb 26, 2020
1 parent 774f2d4 commit 93f96bb
Show file tree
Hide file tree
Showing 9 changed files with 558 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 144.二叉树的前序遍历.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# @lc app=leetcode.cn id=144 lang=python3
#
# [144] 二叉树的前序遍历
#
# https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/
#
# algorithms
# Medium (64.01%)
# Likes: 208
# Dislikes: 0
# Total Accepted: 70.7K
# Total Submissions: 109.9K
# Testcase Example: '[1,null,2,3]'
#
# 给定一个二叉树,返回它的 前序 遍历。
#
# 示例:
#
# 输入: [1,null,2,3]
# ⁠ 1
# ⁠ \
# ⁠ 2
# ⁠ /
# ⁠ 3
#
# 输出: [1,2,3]
#
#
# 进阶: 递归算法很简单,你可以通过迭代算法完成吗?
#
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
# 栈
res = []
if not root:
return res
stack = [root]
while stack:
cur = stack.pop()
res.append(cur.val)
if cur.right:
stack.append(cur.right)
if cur.left:
stack.append(cur.left)
return res
# @lc code=end

57 changes: 57 additions & 0 deletions 145.二叉树的后序遍历.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#
# @lc app=leetcode.cn id=145 lang=python3
#
# [145] 二叉树的后序遍历
#
# https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/
#
# algorithms
# Hard (70.07%)
# Likes: 228
# Dislikes: 0
# Total Accepted: 52.6K
# Total Submissions: 74.8K
# Testcase Example: '[1,null,2,3]'
#
# 给定一个二叉树,返回它的 后序 遍历。
#
# 示例:
#
# 输入: [1,null,2,3]
# ⁠ 1
# ⁠ \
# ⁠ 2
# ⁠ /
# ⁠ 3
#
# 输出: [3,2,1]
#
# 进阶: 递归算法很简单,你可以通过迭代算法完成吗?
#
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
# 栈:前序遍历左右互换后逆序输出
res = []
if not root:
return res
stack = [root]
while stack:
cur = stack.pop()
res.append(cur.val)
if cur.left:
stack.append(cur.left)
if cur.right:
stack.append(cur.right)
return res[::-1]
# @lc code=end

50 changes: 50 additions & 0 deletions 153.寻找旋转排序数组中的最小值.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# @lc app=leetcode.cn id=153 lang=python3
#
# [153] 寻找旋转排序数组中的最小值
#
# https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/description/
#
# algorithms
# Medium (50.19%)
# Likes: 143
# Dislikes: 0
# Total Accepted: 33K
# Total Submissions: 65.8K
# Testcase Example: '[3,4,5,1,2]'
#
# 假设按照升序排序的数组在预先未知的某个点上进行了旋转。
#
# ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
#
# 请找出其中最小的元素。
#
# 你可以假设数组中不存在重复元素。
#
# 示例 1:
#
# 输入: [3,4,5,1,2]
# 输出: 1
#
# 示例 2:
#
# 输入: [4,5,6,7,0,1,2]
# 输出: 0
#
#

# @lc code=start
class Solution:
def findMin(self, nums: List[int]) -> int:
# 二分查找
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return nums[left] # left == right 结束循环 min = left or right

# @lc code=end

58 changes: 58 additions & 0 deletions 199.二叉树的右视图.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# @lc app=leetcode.cn id=199 lang=python3
#
# [199] 二叉树的右视图
#
# https://leetcode-cn.com/problems/binary-tree-right-side-view/description/
#
# algorithms
# Medium (63.19%)
# Likes: 139
# Dislikes: 0
# Total Accepted: 18.4K
# Total Submissions: 29K
# Testcase Example: '[1,2,3,null,5,null,4]'
#
# 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
#
# 示例:
#
# 输入: [1,2,3,null,5,null,4]
# 输出: [1, 3, 4]
# 解释:
#
# ⁠ 1 <---
# ⁠/ \
# 2 3 <---
# ⁠\ \
# ⁠ 5 4 <---
#
#
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
# BFS queue
view = []
if not root:
return view
level = [root]
while level:
view.append(level[-1].val)
for i in range(len(level)):
top = level.pop(0)
if top.left:
level.append(top.left)
if top.right:
level.append(top.right)
return view
# @lc code=end

50 changes: 50 additions & 0 deletions 215.数组中的第k个最大元素.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# @lc app=leetcode.cn id=215 lang=python3
#
# [215] 数组中的第K个最大元素
#
# https://leetcode-cn.com/problems/kth-largest-element-in-an-array/description/
#
# algorithms
# Medium (61.03%)
# Likes: 379
# Dislikes: 0
# Total Accepted: 81.7K
# Total Submissions: 133.5K
# Testcase Example: '[3,2,1,5,6,4]\n2'
#
# 在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
#
# 示例 1:
#
# 输入: [3,2,1,5,6,4] 和 k = 2
# 输出: 5
#
#
# 示例 2:
#
# 输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
# 输出: 4
#
# 说明:
#
# 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。
#
#

# @lc code=start
import heapq

class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
# 最小堆
h = []
for i in range(k):
heapq.heappush(h, nums[i])
for j in range(k, len(nums)):
top = h[0]
if nums[j] > top:
heapq.heapreplace(h, nums[j])
return h[0]
# @lc code=end

96 changes: 96 additions & 0 deletions 232.用栈实现队列.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#
# @lc app=leetcode.cn id=232 lang=python3
#
# [232] 用栈实现队列
#
# https://leetcode-cn.com/problems/implement-queue-using-stacks/description/
#
# algorithms
# Easy (62.72%)
# Likes: 140
# Dislikes: 0
# Total Accepted: 32.9K
# Total Submissions: 52.2K
# Testcase Example: '["MyQueue","push","push","peek","pop","empty"]\n[[],[1],[2],[],[],[]]'
#
# 使用栈实现队列的下列操作:
#
#
# push(x) -- 将一个元素放入队列的尾部。
# pop() -- 从队列首部移除元素。
# peek() -- 返回队列首部的元素。
# empty() -- 返回队列是否为空。
#
#
# 示例:
#
# MyQueue queue = new MyQueue();
#
# queue.push(1);
# queue.push(2);
# queue.peek(); // 返回 1
# queue.pop(); // 返回 1
# queue.empty(); // 返回 false
#
# 说明:
#
#
# 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty
# 操作是合法的。
# 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
# 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
#
#
#

# @lc code=start
class MyQueue:

def __init__(self):
"""
Initialize your data structure here.
"""
self.in_stack, self.out_stack = [], []


def push(self, x: int) -> None:
"""
Push element x to the back of queue.
"""
self.in_stack.append(x)


def pop(self) -> int:
"""
Removes the element from in front of queue and returns that element.
"""
self.move()
return self.out_stack.pop()

def peek(self) -> int:
"""
Get the front element.
"""
self.move()
return self.out_stack[-1]

def empty(self) -> bool:
"""
Returns whether the queue is empty.
"""
return not self.in_stack and not self.out_stack

def move(self):
if not self.out_stack:
while self.in_stack:
self.out_stack.append(self.in_stack.pop())


# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
# @lc code=end

Loading

0 comments on commit 93f96bb

Please sign in to comment.