博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法-树-重建二叉树
阅读量:3965 次
发布时间:2019-05-24

本文共 1107 字,大约阅读时间需要 3 分钟。

在这里插入图片描述

/** * 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 TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder == null || inorder.length == 0) {
return null; } int rootValue = postorder[postorder.length - 1]; int rootIndex = 0; for(int i = 0; i < inorder.length; i++) {
if(inorder[i] == rootValue) {
rootIndex = i; } } TreeNode root = new TreeNode(rootValue); root.left = buildTree(Arrays.copyOfRange(inorder, 0, rootIndex), Arrays.copyOfRange(postorder, 0, rootIndex)); root.right = buildTree(Arrays.copyOfRange(inorder, rootIndex + 1, inorder.length), Arrays.copyOfRange(postorder, rootIndex, postorder.length - 1)); return root; }}

转载地址:http://umhzi.baihongyu.com/

你可能感兴趣的文章
游戏行业了解介绍
查看>>
linux at 命令使用
查看>>
Go在windows下执行命令行指令
查看>>
inotify
查看>>
inode
查看>>
Shell: sh,bash,csh,tcsh等shell的区别
查看>>
golang ubuntu 配置 笔记
查看>>
vim 常用命令
查看>>
golang 开源项目
查看>>
ubntu 开发服务进程
查看>>
linux 常用命令以及技巧
查看>>
记录1年免费亚马逊AWS云服务器申请方法过程及使用技巧
查看>>
golang文章
查看>>
Source Insight 经典教程
查看>>
快速打开菜单附件中的工具
查看>>
Windows系统进程间通信
查看>>
linux exec的用法
查看>>
C语言中如何使用宏
查看>>
Http与RPC通信协议的比较
查看>>
Source Insight的对齐问题
查看>>