LeetCode -- Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:淘宝店提高销量 编辑:程序博客网 时间:2024/04/28 21:55
题目描述:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
就是输入中序遍历(左右中)和后序遍历(左右中)序列,生成二叉树。
思路:
设iFrom, iTo 分别inorder的起始和终止索引; pFrom ,pTo为递归过程中postorder的起始和终止索引。
1. 每次取后序遍历的最后节点,作为当前根节点,即current = new TreeNode(postorder[len - 1])
2. 从inorder序列中找到postorder[len-1]的索引,记为index
3. 创建左子树: inorder的索引范围:[0,index) , postorder的索引范围:[pFrom, pFrom + 距离(index, iFrom) - 1)。
4. 创建右子树: inorder的索引范围: [index + 1, len), postorder 的索引范围: [pFrom + 距离(index, iFrom), pTo-1]。
终止条件:pFrom > pTo 或iFrom > iTo
实现代码:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
就是输入中序遍历(左右中)和后序遍历(左右中)序列,生成二叉树。
思路:
设iFrom, iTo 分别inorder的起始和终止索引; pFrom ,pTo为递归过程中postorder的起始和终止索引。
1. 每次取后序遍历的最后节点,作为当前根节点,即current = new TreeNode(postorder[len - 1])
2. 从inorder序列中找到postorder[len-1]的索引,记为index
3. 创建左子树: inorder的索引范围:[0,index) , postorder的索引范围:[pFrom, pFrom + 距离(index, iFrom) - 1)。
4. 创建右子树: inorder的索引范围: [index + 1, len), postorder 的索引范围: [pFrom + 距离(index, iFrom), pTo-1]。
终止条件:pFrom > pTo 或iFrom > iTo
实现代码:
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode BuildTree(int[] inorder, int[] postorder) {TreeNode node = null;var len = postorder.Length - 1;Build(ref node, inorder, 0, len, postorder, 0, len);return node;}private void Build(ref TreeNode current, int [] inorder, int iFrom, int iTo, int[] postorder, int pFrom, int pTo){if(iFrom > iTo || pFrom > pTo){return;}// set currentcurrent = new TreeNode(postorder[pTo]);// take the last one from post order , because it is the rootvar pLast = postorder[pTo];// find its index in inordervar index = -1;for(var i = iFrom;i <= iTo; i++){if(inorder[i] == pLast){index = i;break;}}// for left sub tree , inorder : [0, index) . postorder : [pFrom, pFrom + distance(index, iFrom) - 1)Build(ref current.left, inorder, iFrom, index - 1, postorder, pFrom, pFrom + index - iFrom - 1);// for right sub tree , inorder : [index + 1, len), postorder : [pFrom + distance(index, iFrom), pTo-1]Build(ref current.right, inorder, index + 1, iTo ,postorder, pFrom + index - iFrom, pTo - 1);}}
0 0
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode - Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- 联萌十一大决战之如日中天 C. Cinderella(灰姑娘新编)
- BZOJ 3231: [Sdoi2008]递归数列
- 变量初始化规则以及声明和定义
- Android Studio 配置优化
- AVL树的思想与C++实现
- LeetCode -- Construct Binary Tree from Inorder and Postorder Traversal
- jsp实现分页和页面跳转功能
- 引擎磨合 (Break In) 的秘密
- C++中有哪些函数不能声明为虚函数
- 线程程序编译出错 undefined reference to `pthread_create'
- jQuery UI基础----2jQuery UI Interractions-draggable(可拖放性
- 拍照图片旋转问题
- LeetCode -- Factorial Trailing Zeroes
- 计算机视觉、机器学习相关领域论文和源代码大集合