495. 实现栈
中文
English
实现一个栈,可以使用除了栈之外的数据结构
Example
例1:
输入:
push(1)
pop()
push(2)
top() // return 2
pop()
isEmpty() // return true
push(3)
isEmpty() // return false
例2:
输入:
isEmpty()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Stack: 2 def __init__(self): 3 self.array = [] 4 """ 5 @param: x: An integer 6 @return: nothing 7 """ 8 def push(self, x): 9 # write your code here 10 self.array.append(x) 11 12 """ 13 @return: nothing 14 """ 15 def pop(self): 16 # write your code here 17 self.array.pop() 18 19 """ 20 @return: An integer 21 """ 22 def top(self): 23 # write your code here 24 return self.array[-1] 25 26 """ 27 @return: True if the stack is empty 28 """ 29 def isEmpty(self): 30 # write your code here 31 return len(self.array) == 0
144. 二叉树的前序遍历
难度中等
给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
递归:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 def preorderTraversal(self, root: TreeNode) -> List[int]: 10 if not root: 11 return list() 12 13 res = list() 14 15 res.append(root.val) 16 res += self.preorderTraversal(root.left) 17 res += self.preorderTraversal(root.right) 18 19 return res 20 21
非递归:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 def preorderTraversal(self, root: TreeNode) -> List[int]: 10 if not root: 11 return list() 12 13 node = root 14 stack = list() 15 res = list() 16 17 while stack or node: 18 if node: 19 res.append(node.val) 20 stack.append(node) 21 node = node.left 22 else: 23 node = stack.pop() 24 node = node.right 25 26 return res 27 28
94. 二叉树的中序遍历
难度中等
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2]
递归:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 def inorderTraversal(self, root: TreeNode) -> List[int]: 10 if not root: 11 return list() 12 13 res = self.inorderTraversal(root.left) 14 res += [root.val] 15 res+= self.inorderTraversal(root.right) 16 17 return res
非递归:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 def inorderTraversal(self, root: TreeNode) -> List[int]: 10 if not root: 11 return list() 12 13 stack = list() 14 node = root 15 res = list() 16 17 while stack or node: 18 if node: 19 stack.append(node) 20 node = node.left 21 22 else: 23 node = stack.pop() 24 res.append(node.val) 25 node = node.right 26 27 return res
145. 二叉树的后序遍历
难度困难
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
递归:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 def postorderTraversal(self, root: TreeNode) -> List[int]: 10 if not root: 11 return list() 12 13 res = self.postorderTraversal(root.left) 14 res += self.postorderTraversal(root.right) 15 res+= [root.val] 16 17 return res
非递归:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 def postorderTraversal(self, root: TreeNode) -> List[int]: 10 if not root: 11 return list() 12 13 stack = [root] 14 res = list() 15 16 while stack: 17 node = stack.pop() 18 res.append(node.val) 19 if node.left: 20 stack.append(node.left) 21 if node.right: 22 stack.append(node.right) 23 24 return res[::-1]
71. 简化路径
难度中等
以 Unix 风格给出一个文件的绝对路径,你需要简化它。或者换句话说,将其转换为规范路径。
在 Unix 风格的文件系统中,一个点(.
)表示当前目录本身;此外,两个点 (..
) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。更多信息请参阅:Linux / Unix中的绝对路径 vs 相对路径
请注意,返回的规范路径必须始终以斜杠 /
开头,并且两个目录名之间必须只有一个斜杠 /
。最后一个目录名(如果存在)不能以 /
结尾。此外,规范路径必须是表示绝对路径的最短字符串。
示例 1:
输入:"/home/" 输出:"/home" 解释:注意,最后一个目录名后面没有斜杠。
示例 2:
输入:"/../" 输出:"/" 解释:从根目录向上一级是不可行的,因为根是你可以到达的最高级。
示例 3:
输入:"/home//foo/" 输出:"/home/foo" 解释:在规范路径中,多个连续斜杠需要用一个斜杠替换。
示例 4:
输入:"/a/./b/../../c/" 输出:"/c"
示例 5:
输入:"/a/../../b/../c//.//" 输出:"/c"
示例 6:
输入:"/a//b////c/d//././/.." 输出:"/a/b/c"
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution: 2 def simplifyPath(self, path: str) -> str: 3 if not path: 4 return '' 5 6 stack = [] 7 path = path.split("/") 8 9 for item in path: 10 if item == "..": 11 if stack : 12 stack.pop() 13 14 elif item and item != ".": 15 stack.append(item) 16 17 return "/" + "/".join(stack)