Construct Binary Tree from Inorder and Postorder Traversal

原创 2015年07月07日 14:14:18
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if (inorder.length != postorder.length) {
            return null;
        }
        return myBuildTree(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
    }
    private TreeNode myBuildTree(int[] inorder, int inBeg, int inEnd, int[] postorder, int postBeg, int postEnd) {
        if (inBeg > inEnd) {
            return null;
        }
        TreeNode root = new TreeNode(postorder[postEnd]);
        int location = findLocation(inorder, inBeg, inEnd, postorder[postEnd]);
        root.left = myBuildTree(inorder, inBeg, location - 1, postorder, postBeg, postBeg + location - inBeg - 1);
        root.right = myBuildTree(inorder, location + 1, inEnd, postorder, postEnd + location - inEnd, postEnd - 1);
        return root;
    }
    private int findLocation(int[] inorder, int begin, int end, int value) {
        for (int i = begin; i <= end; i++) {
            if (inorder[i] == value) {
                return i;
            }
        }
        return -1;
    }  
}

106 Construct Binary Tree from Inorder and Postorder Traversal

题目链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/题目:Giv...
  • ChilseaSai
  • ChilseaSai
  • 2015年12月09日 20:29
  • 864

【LeetCode-面试算法经典-Java实现】【106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)】

【106-Construct Binary Tree from Inorder and Postorder Traversal(通过中序和后序遍历构造二叉树II)】【LeetCode-面试算法经典-J...
  • DERRANTCM
  • DERRANTCM
  • 2015年08月09日 06:39
  • 2194

[LeetCode][Java] Construct Binary Tree from Inorder and Postorder Traversal

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You m...
  • Evan123mg
  • Evan123mg
  • 2015年07月20日 21:23
  • 642

LeetCode Construct Binary Tree from Inorder and Postorder Traversal 思考分析过程分享

思考: 1 画图: 把二叉树和其中序和后序的序列都画出来(这个很重要,我以前有时候总是凭空想象,结果脑子一片空白) 2 把问题写下来:怎么构造一个节点? 3 观察: 到底有什么特点是可以利用来解题的呢...
  • kenden23
  • kenden23
  • 2013年11月12日 13:20
  • 2286

LeetCode --- 106. Construct Binary Tree from Inorder and Postorder Traversal

题目链接:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trave...
  • makuiyu
  • makuiyu
  • 2015年04月17日 16:44
  • 544

LeetCode(106)Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha...
  • feliciafay
  • feliciafay
  • 2014年01月15日 06:20
  • 1118

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016年06月01日 23:42
  • 5860

LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal

LeetCode 105: Given preorder and inorder traversal of a tree, construct the binary tree. 给定一个二叉树的...
  • sunao2002002
  • sunao2002002
  • 2015年05月20日 23:09
  • 1038

C++详解Leetcode:105. Construct Binary Tree from Preorder and Inorder Traversal

原题思路通过二叉树的前序遍历和中序遍历来构建二叉树,通过递归可以很容易的解决这个问题,在遇到二叉树的问题,应该习惯先画图再来解决code/** * Definition for a binary t...
  • u014265347
  • u014265347
  • 2017年07月30日 17:34
  • 304

【LeetCode-面试算法经典-Java实现】【105-Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)】

【106-Construct Binary Tree from Preorder and Inorder Traversal(通过前序和中序遍历构造二叉树)】【LeetCode-面试算法经典-Java...
  • DERRANTCM
  • DERRANTCM
  • 2015年08月09日 06:37
  • 2495
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Construct Binary Tree from Inorder and Postorder Traversal
举报原因:
原因补充:

(最多只允许输入30个字)