今天的华师
Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
思路:
树的层次遍历,技巧是用NULL作为每一行的结尾
问题是什么时候才能是结尾呢?
当遇到队列中front是NULL时,表明上一行已经处理完了,则上一行的子节点也处理完了(处理每个节点的时候,让他的孩子节点入队列),此时把NULL加入队列
当队列中只有一个NULL的时候,则遍历完毕
1 | /** |
Plus One
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
1 | class Solution { |