iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >剑指Offer之Java算法习题精讲二叉树的构造和遍历
  • 484
分享到

剑指Offer之Java算法习题精讲二叉树的构造和遍历

2024-04-02 19:04:59 484人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

题目一 二叉树题——最大二叉树 根据给定的数组来构建最大二叉树 具体题目如下  解法 class Solution { public T

题目一

二叉树题——最大二叉树

根据给定的数组来构建最大二叉树

具体题目如下

 解法


class Solution {
    public Treenode constructMaximumBinaryTree(int[] nums) {
        return method(nums,0,nums.length-1);
    }
    public TreeNode method(int[] nums,int lo,int hi){
        if(lo>hi){
            return null;
        }
        int index = -1;
        int max = Integer.MIN_VALUE;
        for(int i = lo;i<=hi;i++){
            if(max<nums[i]){
                max = nums[i];
                index = i;
            }
        }
        TreeNode root = new TreeNode(max);
        root.left = method(nums,lo,index-1);
        root.right = method(nums,index+1,hi);
        return root;
    }
}

题目二

二叉树题——构造二叉树

根据给定的数组按照指定遍历条件构造二叉树并返回根节点

具体题目如下

 解法


class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return method(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
    }
    public TreeNode method(int[] preorder, int preLeft,int preEnd , int[] inorder,int inLeft,int inEnd){
        if(preLeft>preEnd){
            return null;
        }
        int rootVal = preorder[preLeft];
        int index = -1;
        for(int i = inLeft;i<=inEnd;i++){
            if(rootVal == inorder[i]){
                index = i;
            }
        }
        TreeNode root = new TreeNode(rootVal);
        int leftSize = index - inLeft;
        root.left = method(preorder,preLeft+1,leftSize+preLeft,inorder,inLeft,index-1);
        root.right = method(preorder,leftSize+preLeft+1,preEnd,inorder,index+1,inEnd);
        return root;
    }
}

题目三

二叉树题——构造二叉树

根据给定的数组按照指定遍历条件构造二叉树并返回根节点

具体题目如下

 解法


class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        return build(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
    }
    TreeNode build(int[] inorder, int inStart, int inEnd,int[] postorder, int postStart, int postEnd) {
 
    if (inStart > inEnd) {
        return null;
    }
    // root 节点对应的值就是后序遍历数组的最后一个元素
    int rootVal = postorder[postEnd];
    // rootVal 在中序遍历数组中的索引
    int index = 0;
    for (int i = inStart; i <= inEnd; i++) {
        if (inorder[i] == rootVal) {
            index = i;
            break;
        }
    }
    // 左子树的节点个数
    int leftSize = index - inStart;
    TreeNode root = new TreeNode(rootVal);
    // 递归构造左右子树
    root.left = build(inorder, inStart, index - 1,postorder, postStart, postStart + leftSize - 1);
    root.right = build(inorder, index + 1, inEnd,postorder, postStart + leftSize, postEnd - 1);
    return root;
}
}

题目四

二叉树题——构造二叉树

根据给定的数组按照指定遍历条件构造二叉树并返回

具体题目如下

 解法


class Solution {
    public TreeNode constructFromPrePost(int[] preorder, int[] postorder) {
        return method(preorder,0,preorder.length-1,postorder,0,postorder.length-1);
    }
    public TreeNode method(int[] preorder,int preStart, int preEnd, int[] postorder,int postStart,int postEnd){
        if(preStart>preEnd){
            return null;
        }
        if(preStart==preEnd){
            return new TreeNode(preorder[preStart]);
        }
        int rootVal = preorder[preStart];
        int leftRootVal = preorder[preStart + 1];
        int index = 0;
        for (int i = postStart; i < postEnd; i++) {
            if (postorder[i] == leftRootVal) {
                index = i;
                break;
            }
        }
        TreeNode root = new TreeNode(rootVal);
        int leftSize = index - postStart + 1;
        root.left = method(preorder, preStart + 1, preStart + leftSize,postorder, postStart, index);
        root.right = method(preorder, preStart + leftSize + 1, preEnd,postorder, index + 1, postEnd - 1);
        return root;
    }
}

到此这篇关于剑指Offer之Java算法习题精讲二叉树的构造和遍历的文章就介绍到这了,更多相关Java 二叉树构造内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 剑指Offer之Java算法习题精讲二叉树的构造和遍历

本文链接: https://www.lsjlt.com/news/143335.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作