44: Construct Binary Tree from Inorder and Postorder Traversal

/************************************************************************/
            /*       44:  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.
             * */
            
            /*** 递归做法******************/
            /*
             * 问题:
             *
             * 1: 给出 中序与后序,是否能唯一确定一棵二叉树?(在没有重复节点值的情况下)?
             * 2:  给出 中序与前序,是否能唯一确定一棵二叉树?(在没有重复节点值的情况下)?
             * 自己测试了,出现了不能唯一确定一棵树的情况,待检测
             * */

  public TreeNode buildTreeByPost_In(int[] inorder, int[] postorder)
            {
                 TreeNode root=null;

                 root=helper_PostAndIn(postorder.length-1,0,postorder.length-1,postorder,inorder);
                    return root;
            }

            private  TreeNode helper_PostAndIn(int PostStart, int inStart, int inEnd, int[] postorder, int[] inorder) {
                  if (PostStart < 0 || inStart > inEnd) {
                        return null;
                    }
                    TreeNode root = new TreeNode(postorder[PostStart]);
                    int inIndex = 0; // Index of current root in inorder
                    for (int i = inStart; i <= inEnd; i++) {
                        if (inorder[i] == root.val) {
                            inIndex = i;
                            break;
                        }
                    }
                    root.right = helper_PostAndIn(PostStart-1, inIndex + 1, inEnd, postorder, inorder);
                    root.left = helper_PostAndIn(PostStart-(inEnd-inIndex+1), inStart, inIndex - 1, postorder, inorder);
                    return root;
            }
时间: 2024-10-26 00:27:43

44: Construct Binary Tree from Inorder and Postorder Traversal的相关文章

[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

leetcode 刷题之路 64 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. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not

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

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

Leetcode dfs Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 14363 Total Submissions: 54254My Submissions Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in t

Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: 115870 Given inorder and postorder 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 Inorder and Postorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去.由于后序遍历的最后一个节点就是树的根.也就是roo

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp;amp; 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

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