Jan 28 - Construct Binary Tree From Preorder And Inorder; Tree; DFS; Array

Using DFS to traverse the node and build the a tree. for a node, it has following properties:

If its a left child node of its parent, then the left boundary start of in the inorder array is its parent‘s location in inorder array. Let inorderPos be the location of current node, we can find it in the left part of parent node pos in inorder array. if inorderPos == start+1, this means current node has no left child, set it to be null. Otherwise, it has a left child node, and the postion of its left child node is preorderPos+1 in preorderPos array. Then we can go into its left child, update end to be current node‘s in-order pos. If inorderPos == end - 1; it means current node has no right child, set it to be null. Otherwise, it has a right child node, and the position of its right node is preorderPos+inorderPos-start, update start to be current node‘s in-order pos.

Code:

/**
 * 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[] preorder, int[] inorder) {
        int len = preorder.length;
        if(len == 0) return null;
        return dfsAddNode(preorder, inorder, -1, len, 0);

    }

    public TreeNode dfsAddNode(int[] preorder, int[] inorder, int start, int end, int preorderPos){
        int val = preorder[preorderPos];
        TreeNode node = new TreeNode(val);
        int inorderPos = -1;
        for(int i = start+1; i < end; i++){
            if(inorder[i] == val) inorderPos = i;
        }

        if(inorderPos == start+1) node.left = null;
        else node.left = dfsAddNode(preorder, inorder, start, inorderPos, preorderPos+1);
        if(inorderPos == end-1) node.right = null;
        else node.right = dfsAddNode(preorder, inorder, inorderPos, end, preorderPos+inorderPos-start);

        return node;
    }
    /*
    public int inorderPos(int[] inorder, int start, int end, int val){
        for(int i = start+1; i < end; i++){
            if(inorder[i] == val) return i;
        }
        return -1;
    }
    */
}

A little advance is that, we can use a hashmap to record the postion of inorder element. To avoid unnecessay duplicative look through in the inorder array.

时间: 2024-08-09 23:50:53

Jan 28 - Construct Binary Tree From Preorder And Inorder; Tree; DFS; Array的相关文章

LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. SOLUTION 1: 1. Find the root node from the preorder.(it

【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html 只是子树的前序和中序遍历序列分别更新为: //左子树: left_prestart = prestart+1 lef

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

[LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal

题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

43: Construct Binary Tree from Preorder and Inorder Traversal

/************************************************************************/            /*       43:  Construct Binary Tree from Preorder and Inorder Traversal                            */            /**************************************************

Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 分析:通过一个二叉树的先序遍历和中序遍历可以确定一个二叉树.先序遍历的第一个元素对应二叉树的根结点,由于在中序遍历中左子树和右子树的中序遍历序列分别在根节点两侧,因此我们可以确定左子树和右子树的中序遍历序列.在先序遍历序列中,

[leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根据二叉树的先序遍历和中序遍历恢复二叉树. 解题思路:可以参照 http://www.cnblogs.com/zuoyuan/p/3720138.html 的思路.递归进行解决. 代码: # Definition for a binary tree node # class TreeNode: # d

36. Construct Binary Tree from Inorder and Postorder Traversal &amp;&amp; Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assu

Construct Binary Tree from Preorder and Inorder Traversal leetcode java

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 题解: 1 / \ 2 3 / \ / \ 4 5 6 7 对于上图的树来说, index: 0 1 2 3 4 5 6 先序遍历为: 1 2 4 5 3 6 7 中序遍历为: 4 2 5 1 6 3 7为了清晰表示