码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal

时间:2014-12-05 17:36:45      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:java   leetcode   depth-first search   tree   

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

基本思想和知道前序与中序的思想一样,中序的某一节点的左节点一定是该节点的左子树,而后序遍历的某一节点的左节点一定是该节点的右子树,然后递归


/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
	public TreeNode buildTree(int[] inorder, int[] postorder) {
	    return buildTree(inorder,postorder,0,inorder.length-1,0,postorder.length-1);
	}
	
	private TreeNode buildTree(int []inorder, int []postorder,int inst,int inend,int postst, int postend){
		if(inst>inend||postst>postend||inorder.length<1){
			return null;
		}
		TreeNode tn =new TreeNode(postorder[postend]);
        int index = Arrays.binarySearch(inorder, inst, inend+1, postorder[postend]);
		tn.left = buildTree(inorder,postorder,inst,inst+index-1,postst,postst+index-inst-1);
		tn.right = buildTree(inorder,postorder,index+1,inend,postst+index-inst,postend-1);
		return tn;
	}
}


[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal

标签:java   leetcode   depth-first search   tree   

原文地址:http://blog.csdn.net/guorudi/article/details/41748431

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!