145. Binary Tree Postorder Traversal
问题
Given the root of a binary tree, return the postorder traversal of its nodes’ values.
后序遍历。
先递归左子节点。
然后递归右子节点。
最后将当前节点加入数组。
1 | /** |
145. Binary Tree Postorder Traversal
问题
Given the root of a binary tree, return the postorder traversal of its nodes’ values.
后序遍历。
先递归左子节点。
然后递归右子节点。
最后将当前节点加入数组。
1 | /** |
94. Binary Tree Inorder Traversal
问题
Given the root of a binary tree, return the inorder traversal of its nodes’ values.
中序遍历。
先递归左子节点。
然后将当前节点加入数组。
最后递归右子节点。
1 | /** |
144. Binary Tree Preorder Traversal
问题
Given the root of a binary tree, return the preorder traversal of its nodes’ values.
先序遍历。
先将当前节点加入数组。
然后递归左子节点。
最后递归右子节点。
1 | /** |
232. Implement Queue using Stacks
问题
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).Implement the MyQueue class:
void push(int x) Pushes element x to the back of the queue.
int pop() Removes the element from the front of the queue and returns it.
int peek() Returns the element at the front of the queue.
boolean empty() Returns true if the queue is empty, false otherwise.
Notes:You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack’s standard operations.
创建两个栈。
当入队列时,将元素压入第一个栈。
当出队列或进行其他操作时,如第二个栈为空,则将第一个栈的元素倒出到第二个栈。
此时第二个栈内的内容为顺序。
1 | class MyQueue { |
问题
Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.In one shift operation:
Element at grid[i][j] moves to grid[i][j + 1].
Element at grid[i][n - 1] moves to grid[i + 1][0].
Element at grid[m - 1][n - 1] moves to grid[0][0].
Return the 2D grid after applying shift operation k times.
遍历整个数组,将索引值加上移动的次数,得到新的位置。
1 | class Solution { |