树的基础算法

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;
    }
相关推荐
leo__5205 分钟前
matlab实现非线性Granger因果检验
人工智能·算法·matlab
GG不是gg13 分钟前
位运算详解之异或运算的奇妙操作
算法
我在北国不背锅16 分钟前
基于Java开发的浏览器自动化Playwright-MCP服务器
java·playwright·mcp
陈旭金-小金子19 分钟前
发现 Kotlin MultiPlatform 的一点小变化
android·开发语言·kotlin
Mikhail_G23 分钟前
Python应用八股文
大数据·运维·开发语言·python·数据分析
LUCIAZZZ34 分钟前
钉钉机器人-自定义卡片推送快速入门
java·jvm·spring boot·机器人·钉钉·springboot
景彡先生39 分钟前
C++ 中文件 IO 操作详解
开发语言·c++
优秀1351 小时前
java33
java
你怎么知道我是队长1 小时前
GO语言---defer关键字
开发语言·后端·golang
无影无踪的青蛙1 小时前
[C++] STL大家族之<map>(字典)容器(附洛谷)
开发语言·c++