标题: | |
通过率: | 26.7% |
难度: | 中等 |
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.前面做过一个已经前序和中序求二叉树,本题是一直中序和后续求二叉树道理一样
前序的第一个值一定是树的root,那么后序的最后一个值一定是树的root,
具体看代码:
1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */10 public class Solution {11 public TreeNode buildTree(int[] inorder, int[] postorder) {12 if(inorder.length==0||postorder.length==0)return null;13 TreeNode root=new TreeNode(postorder[postorder.length-1]);14 int i=0;15 for(;i