leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:金十数据官方网站 编辑:程序博客网 时间:2024/06/14 04:29
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
后序遍历的最后一个是根节点,将中序遍历分开成左右子树。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* helper(vector<int> &inorder, int begin1, int end1, vector<int> &postorder, int begin2, int end2) { if (begin2 > end2) return NULL; int lenleft = find(inorder.begin(),inorder.end(),postorder[end2])-inorder.begin()-begin1; TreeNode *p = new TreeNode(postorder[end2]); p->left = helper(inorder,begin1,begin1+lenleft-1,postorder,begin2,begin2+lenleft-1); p->right = helper(inorder,begin1+lenleft+1,end1,postorder,begin2+lenleft,end2-1); return p; } TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { return helper(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1); }};
阅读全文
0 0
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode --- 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106.Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-106.Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode - 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode-106. Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
- 287. Find the Duplicate Number 寻找重复数
- Linux技巧: Linux终端打印如何设置不同的颜色及显示方式
- Nginx负载均衡-如何自定义URL中的hash key
- C++类中的静态成员变量和静态成员函数的作用
- Unity3D的SystemInfo类,用于获取运行设备硬件信息(CPU、显卡、类型等)
- leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- Delphi根据方法名调用方法
- SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
- Windows下VS2013编译Caffe完全手册
- webpack学习资源集合及学习笔记
- Spring Boot -- 微信企业号开发02-启用企业应用的消息服务器
- MFC显示图片和写入文件
- 项目启动后发生org.apache.jasper.JasperException: Failed to load or instantiate TagLibraryValidator class: o
- 嵌入式 Linux 软件更新机制及架构汇总