持续更新中................
二叉树的定义
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode() {
}
public TreeNode(int val) {
this.val = val;
}
public TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
144. 二叉树的前序遍历
给你二叉树的根节点 root
,返回它节点值的 前序 遍历。
示例1:输入:root = [1,null,2,3] 输出:[1,2,3]
//递归遍历
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
preorder(root, list);
return list;
}
public void preorder(TreeNode root, List<Integer> list){
if(root == null){
return;
}
list.add(root.val);
preorder(root.left, list);
preorder(root.right, list);
}
}
//迭代遍历
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null){
return list;
}
// 前序遍历顺序:中-左-右,入栈顺序:中-右-左
Deque<TreeNode> deque = new LinkedList<>();
deque.push(root);
while(!deque.isEmpty()){
TreeNode node = deque.pop();
list.add(node.val);
if(node.right != null){
deque.push(node.right);
}
if(node.left != null){
deque.push(node.left);
}
}
return list;
}
}
//迭代遍历(前序、后序、中序的统一形式)
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Deque<TreeNode> deque = new LinkedList<>();
if(root != null){
deque.push(root);
}
while(!deque.isEmpty()){
TreeNode node = deque.peek();
if(node != null){
deque.pop();
//前序遍历顺序:中-左-右,入栈顺序:右-左-中,出栈顺序:中-左-右
if(node.right != null){
deque.push(node.right);
}
if(node.left != null){
deque.push(node.left);
}
deque.push(node);
deque.push(null); //加入空节点做为标记,来将中节点取出
}else{
deque.pop(); //将空节点弹出
node = deque.peek();
list.add(node.val);
deque.pop();
}
}
return list;
}
}
145. 二叉树的后序遍历
给你一棵二叉树的根节点 root
,返回其节点值的 后序遍历 。
示例1:输入:root = [1,null,2,3] 输出:[3,2,1]
//递归遍历
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
postorder(root, list);
return list;
}
public void postorder(TreeNode root, List<Integer> list){
if(root == null){
return;
}
postorder(root.left, list);
postorder(root.right, list);
list.add(root.val);
}
}
//迭代遍历
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null){
return list;
}
// 后序遍历顺序 左-右-中 入栈顺序:中-左-右 出栈顺序:中-右-左, 最后翻转结果得到 左-右-中
Deque<TreeNode> deque = new LinkedList<>();
deque.push(root);
while(!deque.isEmpty()){
TreeNode node = deque.pop();
list.add(node.val);
if(node.left != null){
deque.push(node.left);
}
if(node.right != null){
deque.push(node.right);
}
}
Collections.reverse(list);
return list;
}
}
//迭代遍历(前序、后序、中序的统一形式)
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Deque<TreeNode> deque = new LinkedList<>();
if(root != null){
deque.push(root);
}
while(!deque.isEmpty()){
TreeNode node = deque.peek();
if(node != null){
deque.pop();
//后序遍历顺序:左-右-中,入栈顺序:中-右-左,出栈顺序:左-右-中
deque.push(node);
deque.push(null); //加入空节点做为标记,来将节点取出
if(node.right != null){
deque.push(node.right);
}
if(node.left != null){
deque.push(node.left);
}
}else{
deque.pop(); //将空节点弹出
node = deque.peek();
list.add(node.val);
deque.pop();
}
}
return list;
}
}
94. 二叉树的中序遍历
给定一个二叉树的根节点 root
,返回 它的 中序 遍历 。
示例1:输入:root = [1,null,2,3] 输出:[1,3,2]
//递归遍历
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
inorder(root, list);
return list;
}
public void inorder(TreeNode root, List<Integer> list){
if(root == null){
return;
}
inorder(root.left, list);
list.add(root.val);
inorder(root.right, list);
}
}
//迭代遍历
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null){
return list;
}
//中序遍历顺序: 左-中-右 入栈顺序: 左-右
Deque<TreeNode> deque = new LinkedList<>();
TreeNode node = root;
while(node != null || !deque.isEmpty()){
if(node != null){
deque.push(node);
node = node.left;
}else{
node = deque.pop();
list.add(node.val);
node = node.right;
}
}
return list;
}
}
//迭代遍历(前序、后序、中序的统一形式)
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Deque<TreeNode> deque = new LinkedList<>();
if(root != null){
deque.push(root);
}
while(!deque.isEmpty()){
TreeNode node = deque.peek();
if(node != null){
deque.pop();
//中序遍历顺序:左-中-右,入栈顺序:右-中-左,出栈顺序:左-中-右
if(node.right != null){
deque.push(node.right);
}
deque.push(node);
deque.push(null); //加入空节点做为标记,来将节点取出
if(node.left != null){
deque.push(node.left);
}
}else{
deque.pop(); //将空节点弹出
node = deque.peek();
list.add(node.val);
deque.pop();
}
}
return list;
}
}
102. 二叉树的层序遍历
给你二叉树的根节点 root
,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
示例1:输入:root = [3,9,20,null,null,15,7] 输出:[[3],[9,20],[15,7]]
//递归遍历
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
levelOrderTraversal(root, list, 0);
return list;
}
public void levelOrderTraversal(TreeNode root, List<List<Integer>> list, Integer deep){
if(root == null){
return;
}
deep++;
if(list.size() < deep){
List<Integer> item = new ArrayList<>();
list.add(item);
}
list.get(deep - 1).add(root.val);
levelOrderTraversal(root.left, list, deep);
levelOrderTraversal(root.right, list, deep);
}
}
//迭代遍历
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if(root == null){
return list;
}
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root);
while(!deque.isEmpty()){
List<Integer> item = new ArrayList<>();
int len = deque.size();
for(int i = 0; i < len; i++){
TreeNode node = deque.poll();
item.add(node.val);
if(node.left != null){
deque.offer(node.left);
}
if(node.right != null){
deque.offer(node.right);
}
}
list.add(item);
}
return list;
}
}
107. 二叉树的层序遍历II
给你二叉树的根节点 root
,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
示例1:输入:root = [3,9,20,null,null,15,7] 输出:[[15,7],[9,20],[3]]
//递归遍历
class Solution107 {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
levelOrderTraversal(root, list, 0);
Collections.reverse(list);
return list;
}
public void levelOrderTraversal(TreeNode root, List<List<Integer>> list, Integer deep){
if(root == null){
return;
}
deep++;
if(list.size() < deep){
List<Integer> item = new ArrayList<>();
list.add(item);
}
list.get(deep - 1).add(root.val);
levelOrderTraversal(root.left, list, deep);
levelOrderTraversal(root.right, list, deep);
}
}
//迭代遍历
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if(root == null){
return list;
}
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root);
while(!deque.isEmpty()){
List<Integer> item = new ArrayList<>();
int len = deque.size();
for(int i = 0; i < len; i++){
TreeNode node = deque.poll();
item.add(node.val);
if(node.left != null){
deque.offer(node.left);
}
if(node.right != null){
deque.offer(node.right);
}
}
list.add(item);
}
Collections.reverse(list);
return list;
}
}
199. 二叉树的右视图
给定一个二叉树的 根节点 root
,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例1:输入: [1,2,3,null,5,null,4] 输出: [1,3,4]
//递归遍历
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
List<List<Integer>> list = new ArrayList<>();
levelOrderTraversal(root, list, 0);
//先层序遍历,再取每一层最右侧的值
for(List<Integer> item : list){
result.add(item.get(item.size() - 1));
}
return result;
}
public void levelOrderTraversal(TreeNode root, List<List<Integer>> list, Integer deep){
if(root == null){
return;
}
deep++;
if(list.size() < deep){
List<Integer> item = new ArrayList<>();
list.add(item);
}
list.get(deep - 1).add(root.val);
levelOrderTraversal(root.left, list, deep);
levelOrderTraversal(root.right, list, deep);
}
}
//迭代遍历
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null){
return list;
}
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root);
while(!deque.isEmpty()){
int len = deque.size();
for(int i = 0; i < len; i++){
TreeNode node = deque.poll();
if(i == len - 1){
list.add(node.val);
}
if(node.left != null){
deque.offer(node.left);
}
if(node.right != null){
deque.offer(node.right);
}
}
}
return list;
}
}
637. 二叉树的层平均值
给定一个非空二叉树的根节点 root
, 以数组的形式返回每一层节点的平均值。
示例1: 输入:root = [3,9,20,null,null,15,7] 输出:[3.00000,14.50000,11.00000]
//递归遍历
class Solution637 {
public List<Double> averageOfLevels(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
levelOrderTraversal(root, list, 0);
List<Double> result = new ArrayList<>();
for(List<Integer> item : list){
double sum = 0;
for(int i : item){
sum += i;
}
result.add(sum / item.size());
}
return result;
}
public void levelOrderTraversal(TreeNode root, List<List<Integer>> list, Integer deep){
if(root == null){
return;
}
deep++;
if(list.size() < deep){
List<Integer> item = new ArrayList<>();
list.add(item);
}
list.get(deep - 1).add(root.val);
levelOrderTraversal(root.left, list, deep);
levelOrderTraversal(root.right, list, deep);
}
}
//迭代遍历
class Solution {
public List<Double> averageOfLevels(TreeNode root) {
List<Double> list = new ArrayList<>();
if(root == null){
return list;
}
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root);
while(!deque.isEmpty()){
double sum = 0;
int len = deque.size();
for(int i = 0; i < len; i++){
TreeNode node = deque.poll();
sum += node.val;
if(node.left != null){
deque.offer(node.left);
}
if(node.right != null){
deque.offer(node.right);
}
}
list.add(sum / len);
}
return list;
}
}
429. N叉树的层序遍历
给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。
树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。
示例1:输入:root = [1,null,3,2,4,null,5,6] 输出:[[1],[3,2,4],[5,6]]
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int val) {
this.val = val;
}
public Node(int val, List<Node> children) {
this.val = val;
this.children = children;
}
}
//递归
class Solution {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> list = new ArrayList<>();
levelOrderTraversal(root, list, 0);
return list;
}
public void levelOrderTraversal(Node root, List<List<Integer>> list, Integer deep){
if(root == null){
return;
}
deep++;
if(list.size() < deep){
List<Integer> item = new ArrayList<>();
list.add(item);
}
list.get(deep - 1).add(root.val);
for(Node child : root.children){
levelOrderTraversal(child, list, deep);
}
}
}
//迭代
class Solution {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> list = new ArrayList<>();
if(root == null){
return list;
}
Deque<Node> deque = new LinkedList<>();
deque.offer(root);
while(!deque.isEmpty()){
List<Integer> item = new ArrayList<>();
int len = deque.size();
for(int i = 0; i < len; i++){
Node node = deque.poll();
item.add(node.val);
for(Node child : node.children){
deque.offer(child);
}
}
list.add(item);
}
return list;
}
}
515. 在每个树行中找最大值
给定一棵二叉树的根节点 root
,请找出该二叉树中每一层的最大值。
示例1:输入: root = [1,3,2,5,3,null,9] 输出: [1,3,9]
//递归
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> result = new ArrayList<>();
List<List<Integer>> list = new ArrayList<>();
levelOrderTraversal(root, list, 0);
for(List<Integer> item : list){
result.add(Collections.max(item));
}
return result;
}
public void levelOrderTraversal(TreeNode root, List<List<Integer>> list, int deep){
if(root == null){
return;
}
deep++;
if(list.size() < deep){
List<Integer> item = new ArrayList<>();
list.add(item);
}
list.get(deep - 1).add(root.val);
levelOrderTraversal(root.left, list, deep);
levelOrderTraversal(root.right, list, deep);
}
}
//迭代
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> result = new ArrayList<>();
if(root == null){
return result;
}
Deque<TreeNode> deque = new LinkedList<>();
deque.offer(root);
while(!deque.isEmpty()){
int maxValue = Integer.MIN_VALUE;
int len = deque.size();
for(int i = 0; i < len; i++){
TreeNode node = deque.poll();
maxValue = node.val > maxValue? node.val : maxValue;
if(node.left != null){
deque.offer(node.left);
}
if(node.right != null){
deque.offer(node.right);
}
}
result.add(maxValue);
}
return result;
}
}
116. 填充每个节点的下一个右侧节点指针
给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
示例1:输入:root = [1,2,3,4,5,6,7] 输出:[1,#,2,3,#,4,5,6,7,#]
class Solution {
public Node connect(Node root) {
if(root == null){
return null;
}
Deque<Node> deque = new LinkedList<>();
deque.offer(root);
while(!deque.isEmpty()){
int len = deque.size();
for(int i = 0; i < len; i++){
Node node = deque.poll();
if(i < len - 1){
node.next = deque.peek();
}
if(node.left != null){
deque.offer(node.left);
}
if(node.right != null){
deque.offer(node.right);
}
}
}
return root;
}
}
public class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int val) {
this.val = val;
}
public Node(int val, Node left, Node right, Node next) {
this.val = val;
this.left = left;
this.right = right;
this.next = next;
}
}
117. 填充每个节点的下一个右侧节点指针II
给定一个二叉树,填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。初始状态下,所有 next 指针都被设置为 NULL。
示例1:输入:root = [1,2,3,4,5,null,7] 输出:[1,#,2,3,#,4,5,7,#]
class Solution {
public Node connect(Node root) {
if(root == null){
return null;
}
Deque<Node> deque = new LinkedList<>();
deque.offer(root);
while(!deque.isEmpty()){
int len = deque.size();
for(int i = 0; i < len; i++){
Node node = deque.poll();
if(i < len - 1){
node.next = deque.peek();
}
if(node.left != null){
deque.offer(node.left);
}
if(node.right != null){
deque.offer(node.right);
}
}
}
return root;
}
}