【construct-binary-tree-from-preorder-and-inorder-traversal】
来源:互联网 发布:域名证书提供商 编辑:程序博客网 时间:2024/06/06 16:13
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 inLen = inorder.size();int postLen = postorder.size();return dfs(inorder, 0, inLen-1, postorder, 0, postLen-1);}TreeNode* dfs(vector<int>& inorder, int inStart, int inEnd,vector<int>& postorder, int postStart, int postEnd){if (inStart>inEnd){return NULL;}TreeNode* root = new TreeNode(postorder[postEnd]);int mid;for (mid=inStart; mid<inEnd; mid++){if (inorder[mid]==root->val){break;}}int leftLen = mid - inStart;root->left = dfs(inorder, inStart, mid - 1, postorder, postStart, postStart + leftLen - 1);root->right = dfs(inorder, mid + 1, inEnd, postorder, postStart + leftLen, postEnd - 1);}};
阅读全文
0 0
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal & Construct Binary Tree from Inorder and P
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- System.ArgumentException: 回发或回调参数无效。在配置中使用 < pages enableEventValidation="true"/>
- 手电筒点亮或者关闭事件
- 手机滑屏tab菜单自适应高度,菜单内容级联滑动(tab超过屏幕宽度可自动滑动)
- 对称加密
- Java菜鸟学习日记34
- 【construct-binary-tree-from-preorder-and-inorder-traversal】
- 数学笔记——导数2(求导法则和高阶导数)
- 欢迎使用CSDN-markdown编辑器
- 经典排序算法--选择排序
- tensorflow cifar10 代码详解
- <C语言>“获取时间”函数的定义与封装
- Linux下Web服务器网站故障分析常用的命令
- Windows程序设计-键盘
- 什么是数据结构和算法