树的基础算法

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;
    }
相关推荐
向哆哆1 分钟前
Java 项目管理工具:Maven 与 Gradle 的深度对比与选择
java·开发语言·maven
怪侠沈剑心5 分钟前
海康NVR录像回放SDK原始流转FLV视频流:基于Java的流媒体转码(无需安装第三方插件ffmpeg)
java·开发语言·前端
钟烁卓8 分钟前
C++日志
开发语言·c++
ss27314 分钟前
基于Springboot + vue3实现的流动摊位管理系统
java·spring boot·后端
Pafey19 分钟前
在 Qt 中实现动态切换主题(明亮和暗黑)
开发语言·qt
啊吧怪不啊吧21 分钟前
C++之初识模版
开发语言·数据结构·c++
YKPG25 分钟前
C++学习-入门到精通-【7】类的深入剖析
c++·学习·算法
nvvas33 分钟前
Java Collection(集合) 接口
java·maven
vivo互联网技术1 小时前
vivo官网APP首页端智能业务实践
前端·深度学习·算法
JK0x071 小时前
代码随想录算法训练营 Day49 图论Ⅰ 深度优先与广度优先
算法·深度优先·图论