二叉数-前序遍历
使用迭代与递归完成
import java.util.*;
/**
* @auther SQ
*/
public class TreeTestDemo {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.left.left = new TreeNode(3);
root.right = new TreeNode(4);
root.right.left = new TreeNode(5);
root.right.right = new TreeNode(6);
// 前序遍历
List<Integer> preRes = perOrder(root);
List<Integer> preDfsRes = perOrderDfs(root);
System.out.println("迭代前序遍历"+preRes);
System.out.println("dfs前序遍历"+preDfsRes);
List<Integer> inRes = inOrder(root);
List<Integer> inDfsRes = inOrderDfs(root);
System.out.println("迭代中序遍历"+inRes);
System.out.println("dfs中序遍历"+inDfsRes);
List<Integer> postRes = postOrder(root);
List<Integer> postDfsRes = postOrderDfs(root);
System.out.println("迭代中序遍历"+postRes);
System.out.println("迭代后序遍历"+postDfsRes);
}
/**
* 前序遍历
*
* @param root
* @return
*/
public static List<Integer> perOrder(TreeNode root) {
if (root == null) {
return null;
}
Stack<TreeNode> stack = new Stack<>();
List<Integer> res = new ArrayList<>();
while (stack.size() != 0 || root != null) {
// 前序遍历的顺序 : root - left - right
if (root != null) {
stack.push(root);
res.add(root.val);
root = root.left;
} else {
root = stack.pop();
root = root.right;
}
}
return res;
}
/**
* 前序遍历 使用递归
* root -> left -> right
*
* @param root
* @return
*/
public static List<Integer> perOrderDfs(TreeNode root) {
if (root == null) {
return null;
}
List<Integer> res = new ArrayList<>();
perDfs(root, res);
return res;
}
public static void perDfs(TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
//root -> left -> right
res.add(root.val);
perDfs(root.left,res);
perDfs(root.right,res);
}
}
二叉数-中序遍历
使用迭代与递归完成
/**
* 中序遍历
*
* @param root
* @return
*/
public static List<Integer> inOrder(TreeNode root) {
if (root == null) {
return null;
}
Stack<TreeNode> stack = new Stack<>();
List<Integer> res = new ArrayList<>();
while (stack.size() != 0 || root != null) {
// 中序遍历: left - root - right
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
res.add(root.val);
root = root.right;
}
}
return res;
}
/**
* 中序遍历 递归方式
* @param root
* @return
*/
public static List<Integer> inOrderDfs(TreeNode root) {
if(root == null){
return null;
}
List<Integer> res = new ArrayList<>();
inDfs(root,res);
return res;
}
public static void inDfs(TreeNode root,List<Integer> res){
if(root == null){
return;
}
// 中序排序 left -> root -> right
inDfs(root.left,res);
res.add(root.val);
inDfs(root.right,res);
}
二叉数-后序遍历
使用迭代与递归完成
/**
* 后序遍历 [3, 2, 5, 6, 4, 1]
*
* @param root
* @return
*/
public static List<Integer> postOrder(TreeNode root) {
if (root == null) {
return null;
}
Stack<TreeNode> stack = new Stack<>();
List<Integer> res = new ArrayList<>();
while (stack.size() != 0 || root != null) {
// 后序遍历: left - right - root
// 思路 反正取数 先取root 在去右边 在去左边 最后在反转
if (root != null) {
res.add(root.val);
stack.push(root);
root = root.right;
} else {
root = stack.pop();
root = root.left;
}
}
Collections.reverse(res);
return res;
}
/**
* 后序遍历 递归方式
* @param root
* @return
*/
public static List<Integer> postOrderDfs(TreeNode root) {
if(root == null){
return null;
}
List<Integer> res = new ArrayList<>();
postDfs(root,res);
return res;
}
public static void postDfs(TreeNode root,List<Integer> res){
if(root == null){
return;
}
// 后序排序 left -> right -> root
postDfs(root.left,res);
postDfs(root.right,res);
res.add(root.val);
}
二叉数-leetcode98验证二叉搜索树
class Solution {
/**
*题意要求范围Integer.MIN_VALUE - Integer.MAX_VALUE范围内,所以选择long型
* long范围 -2^64 ~ 2^64-1
* int范围 -2^31 ~ 2^64-1
**/
public boolean isValidBST(TreeNode root) {
return dfs(root,Long.MIN_VALUE,Long.MAX_VALUE);
}
public boolean dfs(TreeNode root,long lower,long upper) {
if(root == null){
return true;
}
//结束条件 当前节点的值小于最小值或者大于最大值则返回false
if(root.val <= lower || root.val >= upper){
return false;
}
return dfs(root.left,lower,root.val) && dfs(root.right, root.val, upper);
}
//使用中序遍历
public boolean isValidBST01(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
List<Integer> res = new ArrayList<>();
while (root != null || !stack.isEmpty() ) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
if(!res.isEmpty()){
if(root.val <= res.get(res.size()-1)){
return false;
}
}
res.add(root.val);
root = root.right;
}
}
return true;
}
}
二叉数-leetcode102. 二叉树的层序遍历

BFS 遍历:BFS(广度优先搜索)「层序遍历」
BFS 遍历使用队列数据结构
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
// 使用BFS 广度优先搜索
// 使用队列
List<List<Integer>> ans = new ArrayList<>();
if(root == null){
return ans;
}
Queue<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
while (queue.size() != 0) {
List<Integer> res = new ArrayList<>();
int n = queue.size();
for (int i = 0; i < n; i++) {
TreeNode poll = queue.poll();
res.add(poll.val);
if (poll.left != null) {
queue.add(poll.left);
}
if (poll.right != null) {
queue.add(poll.right);
}
}
ans.add(res);
}
return ans;
}
}
二叉数-leetcode105. 前序遍历与中序遍历构造二叉树

需要对前序遍历与中序遍历熟悉,文字最先放置了二叉树三种遍历方式的代码。
/**
* 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 buildTree(int[] preorder, int[] inorder) {
return dfs(preorder, 0, preorder.length,
inorder, 0, inorder.length);
}
public TreeNode dfs(int[] preorder, int p_start, int p_end, int[] inorder, int i_start, int i_end) {
//1 根据前序(3,9,20,15,7)能确定根节点为3
if (p_start == p_end) {
return null;
}
// 找到根节点的值,构造root
int root_val = preorder[p_start];
TreeNode root = new TreeNode(root_val);
// 使用root的val在inorder找到root节点
// inorder中的root的左边就是left节点的个数
int leftVal = -1;
for (int i = i_start; i < i_end; i++) {
if (inorder[i] == root_val) {
leftVal = i;
break;
}
}
// 左子节点的个数
int leftNum = leftVal - i_start;
// 构造左子节点
TreeNode leftNode = dfs(preorder, p_start + 1, p_start + 1 + leftNum, inorder, i_start, leftVal);
root.left = leftNode;
TreeNode rightNode = dfs(preorder, p_start + 1 + leftNum, p_end, inorder, leftVal + 1, i_end);
root.right = rightNode;
return root;
}
}