《leetCode》:Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:淘宝网页版卖家中心 编辑:程序博客网 时间:2024/06/04 23:22
题目
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.
思路
这个题目再《剑指Offer》中做过,具体见http://blog.csdn.net/u010412719/article/details/49227411这篇博文。
但是,在看到这个题目时,还是思路不过清晰,因此也没能做出来,罪过呀,呜呜!!!
第一步:根据前序遍历和中序遍历,取前序遍历的第一个结点为头结点curhead,然后在中序遍历找到其所在的位置index,并记录下来
第二步:构造当前结点,然后开始构造当前结点的左右节点即可。
实现代码如下:
struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right;};void buildCurrentNode(struct TreeNode **head,int len,int *preorder,int *inorder){ if(len<=0){ return; } if(preorder==NULL||inorder==NULL){ return ; } // int curHeadNodeValue= preorder[0]; //找到当前头结点在中序遍历的位置 int index_curHeadNode=-1; for(int index_node=0;index_node<len;index_node++){ if(curHeadNodeValue==inorder[index_node]){ index_curHeadNode=index_node; break; } } // if(index_curHeadNode==-1){ printf("存在根节点在中序遍历中找不到,因此不能够构建二叉树!!"); } //开始构建当前头结点 *head=(struct TreeNode*)malloc(sizeof(TreeNode)); if((*head)==NULL){ exit(EXIT_FAILURE); } (*head)->val=curHeadNodeValue; (*head)->left=NULL; (*head)->right=NULL; //最后,在此头结点的基础上开始构建其左右节点,不过,值得注意的是:左节点、右节点在原来前序遍历和中序遍历中出现的位置 buildCurrentNode(&((*head)->left),index_curHeadNode,preorder+1,inorder); buildCurrentNode(&((*head)->right),len-(index_curHeadNode+1),preorder+index_curHeadNode+1,inorder+index_curHeadNode+1);} struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) { if(preorderSize!=inorderSize){ return NULL; } struct TreeNode* head=NULL;//用来指示结果中的头节点 int len=preorderSize; buildCurrentNode(&head,len,preorder,inorder); return head;}
1 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
- 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 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
- [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
- MyEclipse自动补全设置
- POJ 2676 Sudoku DFS+回溯+减枝
- PhoneGap 简介及原理
- Android数据存储 —— SharedPreferences
- EventBus3.0使用总结
- 《leetCode》:Construct Binary Tree from Preorder and Inorder Traversal
- enum与typedef enum的用法
- 极光推送的使用步骤
- Java从List中直接读取未知对象属性数据
- Java中的字节输入出流和字符输入输出流
- Android 开发之 ---- 底层驱动开发(一)
- Android 分渠道打包原理
- Andriod群英传-Android Scroll 分析学习笔记
- iOS本地搜索(谓词)