Construct Binary Tree from Inorder and PreOrder Traversal

preOrder: (root) LLL RRRR

inOrder: LLL (root) RRRR

inOrder:  LLL RRRR (root)

根据preOrder/postOrder 可以知道root(一头一尾); 然后找到root在inorder中对应的位置(index)

index左边即为Left Branch(inorder), 右边是Right Branch(inorder). 确定LeftBranch && RightBranch的长度后,同样可以从PreOrder/PostOrder中获得Left Branch(preorder/postorder) && Right Branch(preorder/postorder)

在分别对leftBranch && right Branch 进行recursive call.

Depth-First Transversal (3 types)

preorder: root --> preorder(root.child) --> preorder(root.right)

inOrder: recursive call on leftBranch --> root --> recursive call on rightBranch

postorder: recursive call on leftBranch--> recursive call on rightBranch-->root

=>Naming regarding the position of root !

  //pre-Condition: root is a valid treeNode

  preorder(Node){

    //boundary case: Node is a leaf

    //recursive case: Node has child(s)

    1. do something to root

    2. if(root.left != null)

       preorder(root.left)

    3. if(root.right != null)

       preorder(root.right)

  }

时间: 2024-10-17 12:18:01

Construct Binary Tree from Inorder and PreOrder Traversal的相关文章

105. Construct Binary Tree from Inorder and preorder Traversal

/* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root一定在中间那个点,还是要找一下中间点的位置 * p.left = construct(preorder, preStart + 1, preStart + (k - inStart),inorder, inStart, k - 1); * p.right = construct(preorder,

LeetCode – Refresh – Construct Binary Tree from Inorder and Preorder Traversal

Only different with preorder and postorder is that the root start from the beginning for preorder. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NUL

36. Construct Binary Tree from Inorder and Postorder Traversal && 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

Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) 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. 递归构建. 思路就是: preorder可以定位到根结点,inorder可以定位左右子树的取值范围. 1. 由

Construct Binary Tree from Inorder and Postorder Traversal (算法课上的题)

Construct Binary Tree from Inorder and Postorder Traversal 这道题之前算法课上好像遇到过,思路也很简单的. 思路:后序序列的最后一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可.(在去除了根节点之后,中序遍历和后序遍历的前N个树都是

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

44: Construct Binary Tree from Inorder and Postorder Traversal

/************************************************************************/            /*       44:  Construct Binary Tree from Inorder and Postorder Traversal                            */            /*************************************************