数据结构的学习历程06

数据结构的学习历程06

    • 二叉树部分习题集
        • 二叉树转字符串
        • 字符串创建二叉树
        • 最近公共祖先
        • 把二叉搜素树转成有序双向链表
        • 根据前与中序遍历构造二叉树
        • 根据中后序遍历结果构建二叉树
        • 非递归前序遍历

二叉树部分习题集

二叉树转字符串

leetcode:二叉树转字符串

    public String tree2str(TreeNode root) {
     
        String str = "";
        if(root == null) return str;
        str = str+root.val;
        if (root.right == null && root.left == null) {
     
            return str;
        }else if (root.right == null &&root.left != null) {
     
            str = str + "("+ tree2str(root.left) + ")";
        }else if (root.right != null && root.left == null){
     
            str = str+"()";
            str = str + "("+ tree2str(root.right) + ")";
        }else if (root.right != null && root.left != null){
     
            str = str + "("+ tree2str(root.left) + ")";
            str = str + "("+ tree2str(root.right) + ")";
        }
        return str;
    }

字符串创建二叉树

leetcode:创建二叉树

public class Main {
     
    static int i=0;
    public static Node creteTree(String str) {
     
        Node root = null;
        if (str.charAt(i)!= '#' ){
     
            root = new Node(str.charAt(i));
            i++;
            root.left = creteTree(str);
            root.right = creteTree(str);
        }else {
     
            i++;
        }
        return root;
    }
    public static void inorder(Node root){
     
        if (root == null) return;
        inorder(root.left);
        System.out.print(root.val+" ");
        inorder(root.right);
    }

    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int i =0;
        Node root = creteTree(str);
        inorder(root);
    }
}

最近公共祖先

LCA问题:最近公共祖先(lowestCommonAncestor)

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
     
        if (root == null) return root;
        if (root == p || root == q) return root;
        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);
        if (left != null && right != null) return root;
        if (left == null && right != null) return right;
        if (left != null && right == null) return left;
        return null;
    }

把二叉搜素树转成有序双向链表

牛客:二叉搜索树转双向链表
leetcode:二叉搜索树转累加树
二叉搜索树:
数据结构的学习历程06_第1张图片

	TreeNode cur = null;
    public void Convert1(TreeNode pRootOfTree) {
     
        if (pRootOfTree == null) return ;
        Convert1(pRootOfTree.left);
        pRootOfTree.left = cur;
        if(cur != null){
     
            cur.right = pRootOfTree;
        }
        cur = pRootOfTree;
        Convert1(pRootOfTree.right);
    }
    public TreeNode Convert(TreeNode pRootOfTree) {
     
        if (pRootOfTree == null) return null;
        Convert1(pRootOfTree);
        while (pRootOfTree.left != null){
     
            pRootOfTree=pRootOfTree.left;
        }
        return pRootOfTree;
    }

根据前与中序遍历构造二叉树

leetcode:根据前与中序遍历构造二叉树

    int k = 0;
    public TreeNode creatTree(int[] preorder,int[] inorder,int b,int e) {
     
        if (b > e) return null;
        TreeNode root = new TreeNode(preorder[k]);//3
        int index = findk(inorder,preorder[k]);//1 0
        k++;
        root.left = creatTree(preorder,inorder,b,index-1);//0,0  2,2
        root.right = creatTree(preorder,inorder,index+1,e);//2,e  
        return root;
    }
    public int findk(int[] inorder , int k){
     
        int i = 0;
        for (; i < inorder.length; i++) {
     
            if (k == inorder[i])
                break;
        }
        return i;
    }
    public TreeNode buildTree(int[] preorder, int[] inorder) {
     
        return creatTree(preorder,inorder,0,preorder.length-1);
    }

根据中后序遍历结果构建二叉树

leetcode:根据中后序遍历结果构建二叉树

    int k = 0;
    public TreeNode creatTree(int[] inorder,int[] postorder,int b,int e) {
     
        if (b > e) return null;
        TreeNode root = new TreeNode(postorder[k]);//3
        int index = findk(inorder,postorder[k]);//1 0
        k--;
        root.right = creatTree(inorder,postorder,index+1,e);//2,e
        root.left = creatTree(inorder,postorder,b,index-1);//0,0  2,2
        return root;
    }
    public int findk(int[] inorder , int k){
     
        int i = 0;
        for (; i < inorder.length; i++) {
     
            if (k == inorder[i])
                break;
        }
        return i;
    }
    public TreeNode buildTree(int[] inorder, int[] postorder) {
     
        k = inorder.length-1;
        return creatTree(inorder,postorder,0,postorder.length-1);
    }

非递归前序遍历

leetcode:非递归前序遍历

    void preOrderTraversal(TreeNode root){
     
        if (root == null) return;
        Stack<TreeNode> sta = new Stack<>();
        sta.push(root);
        while (!sta.empty()){
     
            TreeNode cur = sta.pop();
            System.out.println(cur.val);
            if (cur.right!=null){
     
                sta.push(cur.right);
            }
            if (cur.left!=null){
     
                sta.push(cur.left);
            }
        }
    }

你可能感兴趣的:(数据结构)