105.leetcode Construct Binary Tree from Preorder and Inorder Traversal(medium)[先序、中序构造二叉树]
来源:互联网 发布:知乎内容不重复 编辑:程序博客网 时间:2024/06/05 03:50
Given preorder and inorder traversal of a tree, construct the binary 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* constructTree(vector<int>& preorder,int pstart,int pend, vector<int>& inorder,int istart,int iend) { TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode*)); root->val = preorder[pstart]; root->left = NULL; root->right = NULL; if(pstart == pend&&istart == iend && preorder[pstart] == inorder[istart]) { return root; //此时没有子节点 } int i = istart; for(;i<=iend;i++) { if(inorder[i] == root->val) break; } int len1 = i-istart; if(len1>0) { root->left = constructTree(preorder,pstart+1,pstart+len1,inorder,istart,i-1); } int len2 = iend-i; if(len2>0) { root->right = constructTree(preorder,pstart+1+len1,pend,inorder,i+1,iend); } return root; } TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { if(preorder.size() == 0 && inorder.size() == 0) return NULL; return constructTree(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1); }};
0 0
- 105.leetcode Construct Binary Tree from Preorder and Inorder Traversal(medium)[先序、中序构造二叉树]
- LeetCode-105:Construct Binary Tree from Preorder and Inorder Traversal (利用先序和中序遍历构建二叉树) -- medium
- Construct Binary Tree from Preorder and Inorder Traversal-先序和中序构造树
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal 根据先序序列和中序序列恢复二叉树
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal 先序和中序中恢复二叉树 解题报告
- 106.leetcode.Construct Binary Tree from Inorder and Postorder Traversal(medium)[中序、后序构造二叉树]
- Construct Binary Tree from Preorder and Inorder Traversal 根据先序和中序序列生成二叉树(重重)
- leetCode 105.Construct Binary Tree from Preorder and Inorder Traversal (根据前序遍历和中序遍历构造二叉树)
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal(从前序遍历和中序遍历构造二叉树)
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal(构造二叉树)
- [LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal 根据前序遍历和中序遍历构造二叉树
- LeetCode OJ 之 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
- 【LeetCode笔记】Construct Binary Tree from Preorder and Inorder Traversal 前序、中序还原二叉树
- LeetCode(Construct Binary Tree from Inorder and Postorder Traversal)根据二叉树的中序和后续构造二叉树
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium) (cpp)
- 关于ArrayList 中 subList 方法的陷阱
- poi excel带公式导出
- 出生日记
- 用c实现chmod功能
- Mac 删除mysql
- 105.leetcode Construct Binary Tree from Preorder and Inorder Traversal(medium)[先序、中序构造二叉树]
- java序列化java.io.Externalizable
- AppCan教你从零开始做开发
- android-----JNI中的log打印
- hduoj 2544 最短路(模板 dijkstra + floyd )
- maven项目中加载框架源码
- JVM
- [Linux]知其然且知所以然之grep命令
- Scala学习笔记13 - 结合scala和java