Java实现-中序遍历和后续遍历构建二叉树

Java实现-中序遍历和后续遍历构建二叉树_第1张图片


/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
 
 
public class Solution {
    /**
     *@param inorder : A list of integers that inorder traversal of a tree
     *@param postorder : A list of integers that postorder traversal of a tree
     *@return : Root of a tree
     */
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        // write your code here
        TreeNode root=null;
		root=inAndPost(inorder, postorder);
		return root;

    }
    private static TreeNode inAndPost(int []inorder,int []postorder){
		if(inorder.length==0||postorder.length==0){
			return null;
		}
		int val=postorder[postorder.length-1];
		TreeNode root=new TreeNode(val);
		int index=0;
		for(int i=0;i




你可能感兴趣的:(遍历,中序遍历和后序遍历构建二叉树,斩杀LintCode,All,in,One,LintCode)