树的基础算法

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;
    }
相关推荐
c#上位机几秒前
C#异步编程之async、await
开发语言·c#
苹果醋33 分钟前
Java设计模式实战:从面向对象原则到架构设计的最佳实践
java·运维·spring boot·mysql·nginx
智者知已应修善业11 分钟前
【删除有序数组中的重复项 II之O(N)算法】2024-1-31
c语言·c++·经验分享·笔记·算法
郑州光合科技余经理13 分钟前
实战分享:如何构建东南亚高并发跑腿配送系统
java·开发语言·javascript·spring cloud·uni-app·c#·php
爱装代码的小瓶子14 分钟前
【c++进阶】C++11新特性:一切皆可{}初始化
开发语言·c++·visual studio
yaoxin52112317 分钟前
273. Java Stream API - Stream 中的中间操作:Mapping 操作详解
java·开发语言·python
技术小甜甜18 分钟前
[Python实战] 告别浏览器驱动烦恼:用 Playwright 优雅实现网页自动化
开发语言·python·自动化
vortex518 分钟前
Bash 替换机制(一):命令替换与进程替换
开发语言·chrome·bash
patrickpdx20 分钟前
leetcode:环形链表
算法·leetcode·链表
一念一花一世界21 分钟前
Arbess从基础到实践(25) - 集成GitLab+阿里云OSS实现Java项目自动化构建并将制品上传Aliyun OSS
java·阿里云·gitlab·cicd·arbess