如果对你有帮助的话
为博主点个赞吧
点赞是对博主最大的鼓励
爱心发射~
代码随想录知识星球
最底层
节点可能没填满
外,其余每层
节点数都达到最大值
,并且最下面一层
的节点都集中在该层最左边
的若干位置。若最底层为第 h 层,则该层包含 1~ 2^(h-1)
个节点。前面介绍的树,都没有数值的,而二叉搜索树是有数值的了,二叉搜索树是一个有序树。
平衡二叉搜索树:又被称为AVL(Adelson-Velsky and Landis)树,且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。
前序遍历(递归法,迭代法)
中序遍历(递归法,迭代法)
后序遍历(递归法,迭代法)
层次遍历(迭代法
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
preorder(root, res);
return res;
}
public void preorder(TreeNode root,List<Integer> res){
if(root == null){
return;
}
res.add(root.val);
preorder(root.left,res);
preorder(root.right,res);
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
/*
模拟栈stack——中左右
- 先将头结点进栈
- 判断栈中是否为空
- 不为空, 设置树
- 输出
- 判断左是否为空
- 判断右是否为空
*/
List<Integer> res = new ArrayList<>();
if(root == null){
return res;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
res.add(node.val);
if(node.right != null){
stack.push(node.right);
}
if(node.left != null){
stack.push(node.left);
}
}
return res;
}
}
力扣
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
/*
递归
- 判断是否为空
- 左右后
*/
List<Integer> res = new ArrayList<>();
postorder(root, res);
return res;
}
public void postorder(TreeNode root, List<Integer> res){
if(root == null){
return;
}
postorder(root.left, res);
postorder(root.right, res);
res.add(root.val);
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
/*
迭代:
- 判断是否为空
- 栈stack
- 进栈 中右左
*/
List<Integer> res = new ArrayList<>();
if(root == null){
return res;
}
Stack<TreeNode> sta1 = new Stack<>();
Stack<TreeNode> sta2 = new Stack<>();
sta1.push(root);
while(!sta1.isEmpty()){
TreeNode node = sta1.pop();
sta2.push(node);
if(node.left != null){
sta1.push(node.left);
}
if(node.right != null){
sta1.push(node.right);
}
}
while(!sta2.isEmpty()){
res.add(sta2.pop().val);
}
return res;
}
}
力扣
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
/*
递归:
- 头是否为0
- 左中右
*/
List<Integer> res = new ArrayList<>();
inorder(root, res);
return res;
}
public void inorder(TreeNode root, List<Integer> res){
if(root == null){
return;
}
inorder(root.left, res);
res.add(root.val);
inorder(root.right, res);
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
/*
迭代:
- 左中右
- 先循环最左边,再找右边
*/
List<Integer> res = new ArrayList<>();
if(root == null){
return res;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(!stack.isEmpty() || cur != null){
while(cur!=null){
stack.push(cur);
cur = cur.left;
}
TreeNode node = stack.pop();
res.add(node.val);
if(node.right != null){
cur = node.right;
}
}
return res;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if(root != null){
stack.push(root);
}
while(!stack.isEmpty()){
TreeNode node = stack.peek();
if(node != null){
stack.pop();
if(node.right != null) stack.push(node.right);
stack.push(node);
stack.push(null);
if(node.left != null) stack.push(node.left);
}else{
stack.pop();
node = stack.pop();
res.add(node.val);
}
}
return res;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
/*
DFS-深度优先算法:
- 先左子树一层一层,再右
*/
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root == null){
return res;
}
dfs(1, root, res);
return res;
}
void dfs(int index, TreeNode root, List<List<Integer>> res){
if(res.size()<index){
res.add(new ArrayList<>());
}
res.get(index-1).add(root.val);
if(root.left != null) dfs(index+1, root.left, res);
if(root.right != null) dfs(index+1, root.right, res);
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
/*
BFS-广度优先算法
- 结果、队列
- 一层一层加入队列,再加入结果
*/
List<List<Integer>> res = new ArrayList<List<Integer>>();
Queue<TreeNode> queue = new ArrayDeque<>(); // 队列
if(root != null){
queue.add(root);
}
while(!queue.isEmpty()){
int n = queue.size(); // 二叉树层数
List<Integer> list = new ArrayList<>();
for(int i=0; i<n; ++i){ // 循环加入每一层的数
TreeNode node = queue.poll();
list.add(node.val);
if(node.left != null) queue.add(node.left); // 存到队列中
if(node.right != null) queue.add(node.right);
}
res.add(list);
}
return res;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null){
return res;
}
Queue<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
while(!queue.isEmpty()){
int n = queue.size();
List<Integer> list = new ArrayList<>();
for(int i=0; i<n; ++i){
TreeNode node = queue.poll();
list.add(node.val);
if(node.left != null) queue.add(node.left);
if(node.right != null) queue.add(node.right);
}
res.add(0,list);
}
return res;
}
}
力扣
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
// BFS
List<Integer> res = new ArrayList<>();
if(root == null){
return res;
}
Queue<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
while(!queue.isEmpty()){
int n = queue.size();
for(int i=0; i<n; ++i){
TreeNode node = queue.poll();
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right);
if(i == n-1){
res.add(node.val);
}
}
}
return res;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
// DFS
dfs(root, 0);
return res;
}
void dfs(TreeNode root, int depth){
if(root == null){
return;
}
if(res.size() == depth){
res.add(root.val);
}
depth++;
dfs(root.right, depth);
dfs(root.left, depth);
}
}
力扣
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Double> averageOfLevels(TreeNode root) {
/*
BFS
- 平均值 = 数总和/数个数
*/
List<Double> res = new ArrayList<>();
if(root == null){
return res;
}
Queue<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
while(!queue.isEmpty()){
double nums = 0;
int n = queue.size();
for(int i=0; i<n; ++i){
TreeNode node = queue.poll();
nums += node.val;
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right);
}
res.add(nums/n);
}
return res;
}
}
/*
// Definition for a Node.
class Node {
public int val;
public List children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List<List<Integer>> levelOrder(Node root) {
// BFS
List<List<Integer>> res = new ArrayList<>();
if(root == null) return res;
Queue<Node> queue = new ArrayDeque<>();
queue.offer(root);
while(!queue.isEmpty()){
int n = queue.size();
List<Integer> list = new ArrayList<>();
for(int i=0; i<n; ++i){
Node node = queue.poll();
list.add(node.val);
for(Node b : node.children){
queue.offer(b);
}
}
res.add(list);
}
return res;
}
}
力扣
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root == null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int n = queue.size();
int max = Integer.MIN_VALUE;
for(int i=0; i<n; ++i){
TreeNode node = queue.poll();
max = Math.max(max, node.val);
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right);
}
res.add(max);
}
return res;
}
}
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
// 层次遍历
if(root == null) return root;
Queue<Node> queue = new LinkedList<Node>();
queue.offer(root);
while(!queue.isEmpty()){
int n = queue.size();
for(int i=0; i<n; ++i){
Node node = queue.poll();
if(i < n-1 ){
node.next = queue.peek();
}
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
}
return root;
}
}
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
// 使用已建立的 next 指针
if(root == null) {
return root;
}
Node leftmost = root;
while(leftmost.left != null){
Node head = leftmost;
// 遍历本层节点
while(head != null){
head.left.next = head.right;
if(head.next != null){
head.right.next = head.next.left;
}
head = head.next;
}
leftmost = leftmost.left;
}
return root;
}
}
力扣
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
// BFS
if(root == null) {
return root;
}
Node node = root;
while(node != null){
Node dummy = new Node(0);
Node head = dummy;
while(node != null){
if(node.left != null){
head.next = node.left;
head = head.next;
}
if(node.right != null){
head.next = node.right;
head = head.next;
}
node = node.next;
}
node = dummy.next;
}
return root;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
// BFS
if(root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<>();
int deep = 0;
queue.offer(root);
while(!queue.isEmpty()){
int n = queue.size();
for(int i=0; i<n; ++i){
TreeNode node = queue.poll();
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
deep++;
}
return deep;
}
}
力扣
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
// BFS
/*
- 定义队列、
- 当队列不为0
- 先循环每层的节点
- 当节点的两个左右子节点都为null,返回deep
*/
if(root == null) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<>();
int deep = 0;
queue.offer(root);
while(!queue.isEmpty()){
int n = queue.size();
deep++;
for(int i=0; i<n; ++i){
TreeNode node = queue.poll();
if(node.left == null && node.right == null){
return deep;
}
if(node.left != null) queue.offer(node.left);
if(node.right != null) queue.offer(node.right);
}
}
return deep;
}
}