剑指 Offer 32 - I. 从上到下打印二叉树
剑指 Offer 32 - II. 从上到下打印二叉树 II
剑指 Offer 32 - III. 从上到下打印二叉树 III
题目
// 32-1
从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回:
[3,9,20,15,7]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
// 32-2
从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回其层次遍历结果:
[
[3],
[9,20],
[15,7]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
// 32-3
请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回其层次遍历结果:
[
[3],
[20,9],
[15,7]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解思路
1>源代码
// 32-1
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
int push_index = 0;
Stack<Integer> stack = new Stack<Integer>();
int pop_index = 0;
int i;
for(i=0;i<pushed.length;i++){
stack.push(pushed[i]);
while(!stack.empty() && stack.peek()==popped[pop_index]){
stack.pop();
pop_index++;
}
}
if(i==pushed.length && stack.empty()){
return true;
}
else{
return false;
}
}
}
// 32-2
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> list = new LinkedList<TreeNode>();
List<List<Integer>> ans = new ArrayList<>();
if(root!=null){
list.add(root);
}
while(!list.isEmpty()){
TreeNode temp = new TreeNode();
List<Integer> temp_l = new ArrayList<>();
for(int i=list.size();i>0;i--){
temp = list.poll();
temp_l.add(temp.val);
if(temp.left != null){
list.add(temp.left);
}
if(temp.right != null){
list.add(temp.right);
}
}
ans.add(temp_l);
}
return ans;
}
}
// 32-3
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> list = new LinkedList<TreeNode>();
List<List<Integer>> ans = new ArrayList<>();
if(root!=null){
list.add(root);
}
while(!list.isEmpty()){
TreeNode temp = new TreeNode();
LinkedList<Integer> temp_l = new LinkedList<>();
for(int i=list.size();i>0;i--){
temp = list.poll();
if(ans.size()%2 == 0) temp_l.addLast(temp.val);
else temp_l.addFirst(temp.val);
if(temp.left != null){
list.add(temp.left);
}
if(temp.right != null){
list.add(temp.right);
}
}
ans.add(temp_l);
}
return ans;
}
}
2>算法介绍
这三道题的核心算法是层次遍历。相信知道层次遍历的算法过程的人一定知道,要想实现层次遍历就要借用队列。在遍历二叉树根节点的时候,首先保存当前节点的值,然后将左孩子右孩子分别进队。之后对于队首的每个节点都执行上述操作直到遍历结束。
第二题要求我们对每一行构建一个单独的列表进行存储,那么我们如何知道某行一共有多少个元素呢?答案就是在进行循环的时候,i从当前队长(list.length)开始递减。
第三题要求我们第一行从左往右,第二行从右往左…这里我们只需要对已经得到的结果ans进行一下判断,就能轻易得出当前是在第几行。需要注意的是,这里我们的temp_l是一个双向队列,既可以从左插又可以从右插,这就解决了我们从右往左、从左往右的问题。