树的基础算法

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;
    }
相关推荐
3Cloudream2 分钟前
LeetCode 003. 无重复字符的最长子串 - 滑动窗口与哈希表详解
算法·leetcode·字符串·双指针·滑动窗口·哈希表·中等
王璐WL9 分钟前
【c++】c++第一课:命名空间
数据结构·c++·算法
aramae16 分钟前
C++ -- 模板
开发语言·c++·笔记·其他
胡耀超16 分钟前
4、Python面向对象编程与模块化设计
开发语言·python·ai·大模型·conda·anaconda
敲键盘的肥嘟嘟左卫门32 分钟前
StringBuilder类的数据结构和扩容方式解读
java
空白到白1 小时前
机器学习-聚类
人工智能·算法·机器学习·聚类
索迪迈科技1 小时前
java后端工程师进修ing(研一版 || day40)
java·开发语言·学习·算法
十碗饭吃不饱1 小时前
net::ERR_EMPTY_RESPONSE
java·javascript·chrome·html5
白初&1 小时前
SpringBoot后端基础案例
java·spring boot·后端
zzzsde1 小时前
【数据结构】队列
数据结构·算法