树的定义:
public static 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;
}
}
深度优先遍历(DFS)(使用递归,底层是栈):
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
//中序遍历
LinkedList result = new LinkedList<Integer>();
inorder(root, result);
return result;
}
//前序遍历
public void preorder(TreeNode root, List<Integer> result){
if(root == null){ return;}
//中左右
result.add(root.val);
preorder(root.left, result);
preorder(root.right, result);
}
//中序遍历
public void inorder(TreeNode root, List<Integer> result){
if(root == null){ return;}
//左中右
inorder(root.left, result);
result.add(root.val);
inorder(root.right, result);
}
//后序遍历
public void postorder(TreeNode root, List<Integer> result){
if(root == null){ return;}
//左右中
postorder(root.left, result);
postorder(root.right, result);
result.add(root.val);
}
}
广度优先遍历(BFS)(底层是队列,可以解决最大深度等问题):
/*
底层是队列,记录每一层的元素个数,把每一层的值都加入队列
先add node
再加入左右子树。循环条件为队列非空。
*/
public List<List<Integer>> BFS_Impl(TreeNode node){
//以queue的方式遍历,先入先出,值存储在List中
Queue<TreeNode> queue = new LinkedList<TreeNode>();
//遍历结果存储在list中
List<List<Integer>> res = new LinkedList<List<Integer>>();
if (node == null){
return res;
}
queue.add(node);
while (!queue.isEmpty()){
int size = queue.size();
List<Integer> List = new ArrayList<>();
for (int i=0; i < size; i++){
TreeNode current = queue.poll();//每次从队列中取出一个节点
List.add(current.val);// 添加当前节点的值到当前层级列表
if (current.left != null){
queue.add(current.left);//左子节点入队
}
if (current.right != null){
queue.add(current.right);//右子节点入队
}
}
res.add(List);
}
return res;
}
二叉树的层序遍历(Level Order Traversal)就是广度优先遍历(Breadth-First Search, BFS)的一种具体实现。
/**
* 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) {
/*
底层是队列:先入先出
每一层是List<Integer>,判断每一层的长度
result.add(level)
*/
//level存放的是TreeNode
Queue<TreeNode> level = new LinkedList();
List<List<Integer>> result = new LinkedList();
if (root == null){
return result;
}
level.add(root);
while (! level.isEmpty()){
//求出每一层的长度
int size = level.size();
//存放每一层的节点值
List<Integer> level_val = new LinkedList();
for(int i=0; i<size; i++){
TreeNode cur = level.poll();//每次从队列中取出一个节点
level_val.add(cur.val);//把节点值加入level_val
//左右子树入队
if (cur.left != null){
level.add(cur.left);
}
if (cur.right != null){
level.add(cur.right);
}
}
result.add (level_val);
}
return result;
}
}
class Solution {
public int maxDepth(TreeNode root) {
if (root == null){
return 0;
}
//按照层序遍历的方式遍历二叉树,返回List的长度即为深度
Queue<TreeNode> queue = new LinkedList<>();
List<List<Integer>> result = new LinkedList<>();
queue.add(root);
while (! queue.isEmpty()){
List<Integer> List = new LinkedList();
int size = queue.size();
for (int i=0; i<size; 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);
}
}
result.add(List);
}
int max = result.size();
return max;
}
}
*/
class Solution {
/*
递归
*/
public TreeNode invertTree(TreeNode root) {
//终止条件:节点为空
if (root == null) return null;
//前序遍历
//交换左右孩子指针
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
//继续递归左右孩子
invertTree(root.left);
invertTree(root.right);
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 boolean isSymmetric(TreeNode root) {
//递归
/*
镜像:左右子树有相同的根;左边的右子树==右边的左子树;
需要比较左子树的左子树和右子树的右子树,以及左子树的右子树和右子树的左子树。
前序遍历;中左右
*/
if (root == null) return true;
boolean result = Ismirror(root.left, root.right);
return result;
}
public boolean Ismirror(TreeNode left, TreeNode right) {
if(left == null && right == null) return true;
if((left == null && right != null) || (left != null && right == null)) return false;
return (left.val == right.val) &&
Ismirror(left.left , right.right) &&
Ismirror(left.right , right.left);
}
}
/**
* 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 {
int ans;
public int diameterOfBinaryTree(TreeNode root) {
ans = 1;
//通过depth递归函数求深度ans(全局变量)
depth(root);
//最大直径=最大路径=深度-1
return ans-1;
}
//递归求深度
public int depth(TreeNode node){
//递归终止条件
if (node == null){
return 0;
}
//前序遍历:中左右
int L = depth(node.left);//左儿子为根的子树的深度
int R = depth(node.right);//右儿子为根的子树的深度
//更新深度
ans = Math.max(ans, L + R +1);
return Math.max(L, R) + 1;//返回该节点为根的子树的深度
}
}
/**
* 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 TreeNode sortedArrayToBST(int[] nums) {
TreeNode result = balance(nums, 0, nums.length-1);
return result;
}
//构造递归函数
//每次都取索引区间中间的值作为根节点
//1.确定递归返回值和参数
public TreeNode balance(int[] nums, int lefIndex, int rightIndex){
//2.确定递归终止条件
if (nums == null || lefIndex > rightIndex){
return null;
}
//3.单层递归逻辑
//每次都取索引区间中间的值作为根节点,若为偶数,则取偏左的那个值
int mid = (lefIndex + rightIndex)/2;
//前序遍历,中左右
TreeNode root = new TreeNode();
root.val = nums[mid];
root.left = balance(nums, lefIndex, mid-1);//一定要改成mid-1,不能写mid
root.right = balance(nums, mid+1, rightIndex);//一定要改成mid+1,不能写mid
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 {
long pre =Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
//二叉搜索树中序遍历时,就是递增序列
if (root == null){
return true;
}
//左
if (!isValidBST(root.left)){
return false;
}
//中,单层递归逻辑,比较root.val > pre,并更新pre
if (root.val <= pre){
return false;
}
pre = root.val;
//右
if (!isValidBST(root.right)){
return false;
}
//若以上条件都满足,说明中序遍历后是递增序列,也就是二叉搜索树
return true;
}
}
package org.Tree;
import java.util.ArrayList;
import java.util.List;
public class LC230_Kth {
public static void main(String[] args) {
TreeNode node1 = new TreeNode(2);
node1.left = new TreeNode(1);
node1.right = new TreeNode(3);
int res = kthSmallest(node1, 2);
System.out.println(res);
}
public static 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;
}
}
public static int kthSmallest(TreeNode root, int k) {
List<Integer> list = new ArrayList<>();
Bianli(root, list);
return list.get(k-1);//对于 k 的位置,第𝑘小的元素在 list 中的索引为 k-1
}
public static void Bianli(TreeNode root, List<Integer> result){
//中序遍历以后,二叉搜索树是一个递增序列
if(root == null) return;
//左中右
if(root.left != null) {Bianli(root.left, result);}
result.add(root.val);
if(root.right != null) {Bianli(root.right, result);}
}
}