方法一:递归法
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ans = new LinkedList<>();
preorder(root,ans);
return ans;
}
public void preorder(TreeNode node,List<Integer> ans){
if(node == null) return;
ans.add(node.val); //先保存根结点的值,再遍历左右子树
preorder(node.left,ans);
preorder(node.right,ans);
}
}
方法二:迭代法
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
//使用迭代法对二叉树进行前中后序遍历,利用的是栈结构
//首先将根入栈,因为栈是FILO,所以要先将右结点入栈,再将左结点入栈
List<Integer> ans = new LinkedList<>();
Deque<TreeNode> stack = new LinkedList<>();
if(root == null) return ans;
stack.push(root);
while (!stack.isEmpty()){
TreeNode node = stack.pop();
ans.add(node.val);
if(node.right != null){
stack.push(node.right);
}
if(node.left != null){
stack.push(node.left);
}
}
return ans;
}
}
方法一:递归法
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ans = new LinkedList<>();
postorder(root,ans);
return ans;
}
public void postorder(TreeNode node,List<Integer> ans){
if(node == null) return;
postorder(node.left,ans);
postorder(node.right,ans);
ans.add(node.val);
}
}
方法二:迭代法
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
//前序遍历返回的ans是根左右,如果调换while中右左入栈的顺序
//就会得到根右左顺序的ans,将ans反转就会得到左右根
List<Integer> ans = new LinkedList<>();
if(root == null) return ans;
Deque<TreeNode> stack = new LinkedList<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
ans.add(node.val);
if(node.left != null){
stack.push(node.left);
}
if(node.right != null){
stack.push(node.right);
}
}
Collections.reverse(ans);
return ans;
}
}
方法一:递归法
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ans = new LinkedList<>();
inorder(root,ans);
return ans;
}
public void inorder(TreeNode node, List<Integer> ans){
if(node == null) return;
inorder(node.left,ans);
ans.add(node.val);
inorder(node.right,ans);
}
}
方法二:迭代法
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
//首先判断当前结点是否为空,不为空将当前结点的左结点入栈,
//若为空,取栈顶元素加入ans,将栈顶元素的右结点作为当前结点
//继续重复上面步骤,直到当前结点为空且栈为空为止。
List<Integer> ans = new LinkedList<>();
if(root == null) return ans;
Deque<TreeNode> stack = new LinkedList<>();
TreeNode node = root;
while (node != null || !stack.isEmpty()){
if(node != null){
stack.push(node);
node = node.left;
}else{
node = stack.pop();
ans.add(node.val);
node = node.right;
}
}
return ans;
}
}
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
//二叉树的层序遍历需要利用队列结构
//队头元素出队时,要将队头元素的左右结点入队
//每一层第一个元素出队前,队列的长度即为该层的元素个数
List<List<Integer>> ans = new LinkedList<>();
if(root == null) return ans;
Deque<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
while (!queue.isEmpty()){
List<Integer> temp = new LinkedList<>();
int len = queue.size();
while (len>0){
TreeNode node = queue.poll();
temp.add(node.val);
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
len--;
}
ans.add(temp);
}
return ans;
}
}
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
//将上一题的结果反转即可
List<List<Integer>> ans = new LinkedList<>();
if(root == null) return ans;
Deque<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
while (!queue.isEmpty()){
int len = queue.size();
List<Integer> temp = new LinkedList<>();
while (len > 0){
TreeNode node = queue.poll();
temp.add(node.val);
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
len--;
}
ans.add(temp);
}
Collections.reverse(ans);
return ans;
}
}
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> ans = new LinkedList<>();
if(root == null) return ans;
Deque<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
TreeNode node;
while (len > 0){
node = queue.poll();
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
if(len == 1){
ans.add(node.val);
}
len--;
}
}
return ans;
}
}
class Solution {
public List<Double> averageOfLevels(TreeNode root) {
List<Double> avg = new LinkedList<>();
Deque<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
double sum = 0;
for (int i = 0; i < len; i++) {
TreeNode node = queue.poll();
sum += node.val;
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
avg.add(sum/len);
}
return avg;
}
}
class Solution {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> ans = new LinkedList<>();
if(root == null) return ans;
Deque<Node> queue = new ArrayDeque<>();
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
List<Integer> temp = new LinkedList<>();
for (int i = 0; i < len; i++) {
Node node = queue.poll();
temp.add(node.val);
for (int j = 0; j < node.children.size(); j++) {
queue.offer(node.children.get(j));
}
}
ans.add(temp);
}
return ans;
}
}
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> ans = new LinkedList<>();
if(root == null) return ans;
Deque<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
int max = queue.peek().val;
while (len > 0){
TreeNode node = queue.poll();
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
max = max > node.val ? max : node.val;
len--;
}
ans.add(max);
}
return ans;
}
}
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。初始状态下,所有 next 指针都被设置为 NULL。
class Solution {
public Node connect(Node root) {
if(root == null)
return null;
Deque<Node> queue = new ArrayDeque<>();
queue.offer(root);
while (!queue.isEmpty()){
int len = queue.size();
for (int i = 0; i < len; i++) {
Node temp = queue.poll();
if(temp.left != null){
queue.offer(temp.left);
}
if(temp.right != null){
queue.offer(temp.right);
}
if(i == len-1){
temp.next = null;
}else {
temp.next = queue.peek();
}
}
}
return root;
}
}
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
Deque<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
int height = 0;
while (!queue.isEmpty()){
int len = queue.size();
for (int i = 0; i < len; i++) {
TreeNode temp = queue.poll();
if(temp.left != null){
queue.offer(temp.left);
}
if(temp.right != null){
queue.offer(temp.right);
}
}
height++;
}
return height;
}
}
class Solution {
public int minDepth(TreeNode root) {
//当遍历到最小深度时,必有一个叶子结点
if(root == null) return 0;
Deque<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
int height = 0;
boolean isFlag = false;
while (!queue.isEmpty()){
int len = queue.size();
height++;
for (int i = 0; i < len; i++) {
TreeNode temp = queue.poll();
if(temp.left == null && temp.right == null){
isFlag = true;
break;
}
if(temp.left != null){
queue.offer(temp.left);
}
if(temp.right != null){
queue.offer(temp.right);
}
}
if(isFlag) return height;
}
return height;
}
}