106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:java工程师任职要求 编辑:程序博客网 时间:2024/06/07 08:23
/** * 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 {private: void build(TreeNode*&root,vector<int>&inorder,int il,int ir,vector<int>&postorder,int pl,int pr) { if(ir>=il&&pr>=pl) { int val=postorder[pr]; int i; root=new TreeNode(val); for(i=il;i<=ir;i++) { if(inorder[i]==val) break; } int lchildlength=i-il; int rchildlength=ir-i; build(root->left,inorder,il,i-1,postorder,pl,pl+lchildlength-1); build(root->right,inorder,i+1,ir,postorder,pl+lchildlength,pr-1); } }public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { int il=inorder.size(); int pl=postorder.size(); if(il==0) return NULL; TreeNode *root; build(root,inorder,0,il-1,postorder,0,pl-1); return root; }};
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
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 第一次启动springboot的辛酸历程
- 定制WinCE标准界面的一种方法
- WPF: RenderTransform特效
- Android中获取正在运行的应用程序-----ActivityManager.RunningAppProcessInfo类详解
- Android M Launcher3启动与工作流程源码浅析
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 替代网盘——花生壳全线私有云存储方案
- 6.1 IKEv2 CLI 配置Anyconnect3.0 to ASA
- 一个.java源文件中可以有多个类吗?(内部类除外)有什么条件?
- Android 不开WiFi获取Mac地址 夜未央
- HDMI编码器E300
- 【Swift-Objc】选择排序
- 安卓指令和命令学习总结
- 一文看懂深度学习与计算机视觉(下)