二叉树7道进阶练习题(含题目链接)

1.二叉树的构建及遍历

牛客链接

import java.util.*;

class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;
    
    public TreeNode(char ch) {
        this.val = ch;
    }
}

public class Main {
    static int i = 0;
    public static TreeNode createTree(String str) {
        if(str == null || str.length() <= 0) {
            return null;
        }
        TreeNode root = null;
        if(str.charAt(i) != '#') {
            root = new TreeNode(str.charAt(i));
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        }else {
            i++;
        }
        return root;
    }
    public static void middle(TreeNode root) {
        if(root == null) return;
        middle(root.left);
        System.out.print(root.val+" ");
        middle(root.right);
    }
    
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        String str = cin.nextLine();
        TreeNode root = createTree(str);
        middle(root);
    }
}

2.二叉树的层序遍历

力扣链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> out = new ArrayList<>();
        if(root == null) return out;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            List<Integer> in = new ArrayList<>();
            int size = queue.size();
            while(size > 0) {
                TreeNode cur = queue.poll();
                in.add(cur.val);
                if(cur.left != null) {
                    queue.add(cur.left);
                }
                if(cur.right != null) {
                    queue.add(cur.right);
                }
                size--;
            }
            out.add(in);
        }
        return out;
    }
}

3.二叉树的最近公共祖先

力扣链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) return null;
        if(p == root || q == root) {
            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) {
            return left;
        }
        if(right != null) {
            return right;
        }

        return null;
    }
}

4.二叉树搜索树转换成排序双向链表

牛客链接

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public TreeNode prev = null;
    public void ConvertChild(TreeNode pCur) {
        if(pCur == null) return;
        ConvertChild(pCur.left);
        pCur.left = prev;
        if(prev != null) {
            prev.right = pCur;
        }
        prev = pCur;
        ConvertChild(pCur.right);
    }
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null) return null;
        ConvertChild(pRootOfTree);
        TreeNode head = pRootOfTree;
        while(head.left != null) {
            head = head.left;
        }
        return head;
    }
}

5.从前序与中序遍历序列构造二叉树

力扣链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    public int preindex = 0;
    public TreeNode buildTreeChild(int[] preorder,int[] inorder,int start,int end) {
        if(start > end) return null;
        TreeNode root = new TreeNode(preorder[preindex]);
        int index = findIndex(preorder[preindex],inorder,start,end);
        preindex++;
        root.left = buildTreeChild(preorder,inorder,start,index-1);
        root.right = buildTreeChild(preorder,inorder,index+1,end);  
        return root;
    }

    public int findIndex(int key,int[] inorder,int start,int end) {
        for(int i = start; i <= end; i++) {
            if(key == inorder[i]) {
                return i;
            }
        }
        return -1;
    }

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(preorder == null || inorder == null) return null;
        if(preorder.length == 0 || inorder.length == 0) return null;
        TreeNode root = buildTreeChild(preorder,inorder,0,inorder.length-1);
        return root;
    }
}

6.从中序与后序遍历序列构造二叉树

力扣链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    public int postIndex = 0;
    public TreeNode buildTreeChild(int[] inorder,int[] postorder,int start,int end) {
        if(start > end) return null;
        TreeNode root = new TreeNode(postorder[postIndex]);
        int index = findIndex(inorder,postorder[postIndex],start,end);
        postIndex--;
        root.right = buildTreeChild(inorder,postorder,index+1,end);
        root.left = buildTreeChild(inorder,postorder,start,index-1);

        return root;
    }

    public int findIndex(int[] inorder,int key,int start,int end) {
        for(int i = start; i <= end; i++) {
            if(inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(inorder == null || postorder == null) return null;
        if(inorder.length == 0 || postorder.length == 0) return null;
        postIndex = postorder.length-1;
        return buildTreeChild(inorder,postorder,0,inorder.length-1);
    }
}

7.根据二叉树创建字符串

力扣链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {


    public void tree2strChild(TreeNode t,StringBuilder sb) {
        if(t == null) return;
        sb.append(t.val);
        if(t.left == null) {
            if(t.right == null) {
                return;
            }else{
                sb.append("()");
            }
        }else {
            sb.append("(");
            tree2strChild(t.left,sb);
            sb.append(")");
        }

        if(t.right == null) {
            return;
        }else{
            sb.append("(");
            tree2strChild(t.right,sb);
            sb.append(")");
        }
    }

    public String tree2str(TreeNode t) {
        StringBuilder sb = new StringBuilder();
        tree2strChild(t,sb);
        return sb.toString();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值