94二叉树的中序遍历
思路:树有左孩子,右孩子,可以看做是一个链表,中序遍历则是:从左子树,再到根节点,再到右子树,所以我们可以调用递归,来实现树的遍历。
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list=new ArrayList<>();
helper(root,list);
return list;
}
public static void helper(TreeNode root,List<Integer> res){
if(root!=null){//根节点不为空
if(root.left!=null){//左孩子不为空
helper(root.left,res);
}
res.add(root.val);//节点值加入结果链表
if(root.right!=null){//右孩子不为空
helper(root.right,res);
}
}
}
}
144二叉树的前序遍历
思路:
与中序遍历类似,同样调用递归,前序遍历的顺序是:先根节点,再到左子树,再到右子树。
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list =new ArrayList<>();
helper(root,list);
return list;
}
public static void helper(TreeNode root,List<Integer> res){
if(root!=null){
res.add(root.val);
if(root.left!=null){
helper(root.left,res);
}
if(root.right!=null){
helper(root.right,res);
}
}
}
}
145二叉树的后序遍历
思路:
后序遍历的顺序是:先左子树,再到根节点,再到右子树。
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list =new ArrayList<>();
helper(root,list);
return list;
}
public static void helper(TreeNode root,List<Integer> res){
if(root!=null){//若根节点不为空
if(root.left!=null){//左孩子不为空
helper(root.left,res);
}
if(root.right!=null){//右孩子不为空
helper(root.right,res);
}
res.add(root.val);//根节点值加入结果集链表
}
}
}
589 N叉树的前序遍历
思路:
与二叉树前序遍历顺序一样,我们这里依旧采用递归,先将根节点值加入结果集链表,接着用for-each循环遍历每一个孩子结点,对每一个孩子结点调用递归,最后得到的是前序遍历结果。
class Solution {
public List<Integer> preorder(Node root) {
List<Integer> list =new ArrayList<>();
helper(root,list);
return list;
}
public static void helper(Node root,List<Integer> res){
if(root!=null){
res.add(root.val);
for(Node child : root.children){//从左到右遍历每一个孩子结点
helper(child,res);
}
}
}
}
590 N叉树的后序遍历
思路:
后序遍历顺序即为先左再右,再到根节点,所以这里先循环遍历孩子结点,再将根节点值存入结果集链表。
class Solution {
public List<Integer> postorder(Node root) {
List<Integer> list=new ArrayList<>();
helper(root,list);
return list;
}
public static void helper(Node root,List<Integer> res){
if(root!=null){
for (Node child: root.children){
helper(child,res);
}
res.add(root.val);
}
}
}
102二叉树的层序遍历
思路:
1.先判断根节点是否为空,若为空则返回一个空的结果集;
2.构造辅助函数,参数为层数,根节点以及结果集链表;
3.看题目要求结果集的大小代表了树的深度,所以当结果集大小小于层数时,需新加一个空的值进结果集链表;
4.第index层的节点值加入结果集链表中位置为index-1处;
5.判断左右孩子是否为空,不为空递归,这里index+1,因为是下一层
import java.util.*;
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if(root==null) {//根节点为空,返回空的结果集
return new ArrayList<List<Integer>>();
}
List<List<Integer>> list = new ArrayList<>();
helper(1,root,list);
return list;
}
public static void helper(int index,TreeNode root,List<List<Integer>> res){
if(res.size()<index){//结果集大小小于层数
res.add(new ArrayList<Integer>());//加入空的数组链表进结果集
}
res.get(index-1).add(root.val);//往结果集index-1加入根节点数值
if(root.left!=null){
helper(index+1,root.left,res);
}
if(root.right!=null){
helper(index+1,root.right,res);
}
}
}
107二叉树的层序遍历II
思路:
这里为逆序输出,从底往上遍历,需要用LinkedList存放结果集,当结果集的长度小于层数时,需要往头部添加空的链表,还需要在原来的层序遍历基础上修改存入根节点值的位置,原来的index-1变为res.size()-index。
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
if(root==null){//如果根节点为空,返回一个空链表
return new LinkedList<List<Integer>>();
}
LinkedList<List<Integer>> list=new LinkedList<List<Integer>>();
helper(1,root,list);
return list;
}
public static void helper(int index,TreeNode root,LinkedList<List<Integer>> res){
if(res.size()<index){//往头部加空链表
res.addFirst(new LinkedList<Integer>());
}
res.get(res.size()-index).add(root.val);
if(root.left!=null){
helper(index+1,root.left,res);
}
if(root.right!=null){
helper(index+1,root.right,res);
}
}
}
429 N叉树的层序遍历
思路:
大致思路与上面的二叉树层序遍历一致,需要注意的是没有左右孩子节点,替代的是children节点,所以需要for-each遍历children节点,再递归。
class Solution {
public List<List<Integer>> levelOrder(Node root) {
if(root==null){
return new ArrayList<List<Integer>>();//如果根节点为空,返回空链表
}
List<List<Integer>> list = new ArrayList<List<Integer>>();
helper(1,root,list);//调用辅助函数
return list;
}
public static void helper(int index,Node root,List<List<Integer>> res){//index表示层数,从1开始
if(res.size()<index){//如果结果集的大小小于当前层数,则加一层空数组链表
res.add( new ArrayList<Integer>());
}
res.get(index-1).add(root.val);//往该层加节点值
for(Node child :root.children){//遍历孩子结点,然后递归调用,index+1因为是孩子结点在下一层
helper(index+1,child,res);
}
}
}
104二叉树的最大深度
思路:
调用递归,根节点为空返回0,不为空返回左右孩子最大的深度加一。
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
}
}
559 N叉树的最大深度
思路:
同样,根节点为空,返回0,根节点的孩子结点为空,返回1,否则,维护一个链表,用于存放每一个孩子结点为根节点的树的深度,最后再用Collections.max方法找出最大值加上1就是N叉树的最大深度。
class Solution {
public int maxDepth(Node root) {
if (root == null) {
return 0;
} else if (root.children.isEmpty()) {
return 1;
} else {
List<Integer> heights = new LinkedList<>();
for (Node item : root.children) {
heights.add(maxDepth(item));
}
return Collections.max(heights) + 1;
}
}
}