树的基础算法

java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
class BiTree {
    BiTree left;
    BiTree right;
    int val;
}
public class TreeDemo {

    public static  void preOrder01(BiTree root){
        if(root == null){
            return;
        }
        System.out.print(root.val+"\t");
        if(root.left != null){
            preOrder01(root.left);
        }
        if(root.right != null){
            preOrder01(root.right);
        }
    }

    public static  void preOrder(BiTree root){
        BiTree p = root;
        LinkedList<BiTree> stack = new LinkedList<>();
        while(p != null || !stack.isEmpty()){
            if(p != null){
                System.out.print(p.val +"\t");
                stack.push(p);
                p = p.left;
            }else {
                p = stack.pop();
                p = p.right;
            }
        }
    }
    public static  void postOrder01(BiTree root){
        if(root == null){
            return;
        }
        if(root.left != null){
            postOrder01(root.left);
        }
        if(root.right != null){
            postOrder01(root.right);
        }
        System.out.print(root.val+"\t");
    }

    public static  void inOrder01(BiTree root){
        if(root == null){
            return;
        }
        if(root.left != null){
            inOrder01(root.left);
        }
        System.out.print(root.val+"\t");
        if(root.right != null){
            inOrder01(root.right);
        }
    }
    public static  void inOrder(BiTree root){
        LinkedList<BiTree> stack = new LinkedList<>();
        BiTree p = root;
        while( p != null || !stack.isEmpty()){
            if(p != null){
                stack.push(p);
                p = p.left;
            }else {
                p = stack.pop();
                System.out.print(p.val + "\t");
                p = p.right;
            }
        }
    }

    public static  void postOrder(BiTree root){
        LinkedList<BiTree> stack = new LinkedList<BiTree>();
        BiTree p = root;
        BiTree r = null;
        while(p != null || !stack.isEmpty()){
            if(p != null){
                stack.push(p);
                p = p.left;
            }else {
                p = stack.peek();
                if (p.right != null && p.right != r) {
                    p = p.right;
                } else {
                    p = stack.pop();
                    System.out.print(p.val + "\t");
                    r = p;
                    p = null;
                }
            }
        }
    }

    public static  void levelOrder(BiTree root){
        LinkedList<BiTree> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            BiTree p = queue.poll();
            System.out.print(p.val +"\t");
            if(p.left != null){
                queue.offer(p.left);
            }
            if(p.right != null){
                queue.offer(p.right);
            }
        }
    }
}
  1. 根据中序和先序确定重建二叉树

  2. 二叉树按层打印

java 复制代码
 public static List<List<Integer>> levelOrder02(BiTree root){
        List<List<Integer>> results = new LinkedList<>();
        LinkedList<BiTree> queue= new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int levelSize = queue.size();
            List<Integer> result = new ArrayList<>();
            for(int i = 1; i <= levelSize; i++){
                BiTree p = queue.poll();
                result.add(p.val);
                if(p.left != null){
                    queue.offer(p.left);
                }
                if(p.right != null){
                    queue.offer(p.right);
                }
            }
            results.add(result);
        }
        return results;
    }
  1. 判断一棵树是否是完全二叉树,二叉排序树,对称二叉树,二叉树的最近公共子祖先。
java 复制代码
 public boolean isValidBST(TreeNode root) {
       return isValidBST(root,Long.MIN_VALUE, Long.MAX_VALUE);
    }
    public boolean  isValidBST(TreeNode root, long lower, long  upper){
        if(root == null){
            return true;
        }
        if(root.val <= lower ||  root.val >= upper){
            return false;
        }
        return isValidBST(root.left,lower, root.val) &&  isValidBST(root.right,root.val, upper);
    }
java 复制代码
   public static boolean isCBT(BiTree root){
        if(root == null){
            return true;
        }
        LinkedList<BiTree> queue  = new LinkedList<>();
        queue.offer(root);
        boolean hasNull = false;
        while(!queue.isEmpty()){
            BiTree node = queue.poll();
            if(node == null){
                hasNull = true;
            }else {
                if(hasNull) {
                    return false;
                }
                queue.offer(node.left);
                queue.offer(node.right);
            }
        }
        return true;
    }
    
java 复制代码
 public BiTree lowestCommonAncestor(BiTree root, TreeNode p, TreeNode q) {
        if (root == null) {
            return null;
        }
        if(root == p || root == q){
            return root;
        }
        BiTree left = lowestCommonAncestor(root.left, p,q);
        BiTree right = lowestCommonAncestor(root.right,p,q);
        if(left != null  &&  right!= null){
            return root;
        }
        if(left != null &&  right == null){
            return left;
        }
        if(right != null &&  left == null){
            return right;
        }
        return null;
    }
相关推荐
云空4 分钟前
《探索电脑麦克风声音采集多窗口实时可视化技术》
人工智能·python·算法
麦兜*8 分钟前
【Spring Boot】Spring Boot 4.0 的颠覆性AI特性全景解析,结合智能编码实战案例、底层架构革新及Prompt工程手册
java·人工智能·spring boot·后端·spring·架构
野犬寒鸦16 分钟前
MyBatis-Plus 中使用 Wrapper 自定义 SQL
java·数据库·后端·sql·mybatis
沧澜sincerely18 分钟前
二分查找【各种题型+对应LeetCode习题练习】
算法·leetcode·二分查找
expect7g24 分钟前
Java的DNS缓存问题
java·后端
oioihoii24 分钟前
C++11中的std::minmax与std::minmax_element:原理解析与实战
java·开发语言·c++
超龄超能程序猿25 分钟前
使用 Python 对本地图片进行图像分类
开发语言·人工智能·python·机器学习·分类·数据挖掘·scipy
大千AI助手28 分钟前
RLHF:人类反馈强化学习 | 对齐AI与人类价值观的核心引擎
人工智能·深度学习·算法·机器学习·强化学习·rlhf·人类反馈强化学习
wkj00129 分钟前
php中调用对象的方法可以使用array($object, ‘methodName‘)?
android·开发语言·php
karry01301 小时前
高并发导致重复key问题--org.springframework.dao.DuplicateKeyException
java·数据库·ide