Construct Binary Tree from Preorder and Inorder Traversal (中序和后序建树,在题目给的函数中完成)【leetcode】
来源:互联网 发布:c语言api函数大全 编辑:程序博客网 时间:2024/05/16 05:36
题目:Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
和中序后序建树一样,解释看上题。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { int len=inorder.size(); if(len==0) return NULL; vector<int> leftin,leftpr,rightin,rightpr; bool flag=0; int p; for(int i=0;i<len;++i) { if(inorder[i]==preorder[0]) { p=i; flag=1; } else if(flag==0) { leftin.push_back(inorder[i]); leftpr.push_back(preorder[i+1]); } else { rightin.push_back(inorder[i]); rightpr.push_back(preorder[i]); } } TreeNode *root=new TreeNode(inorder[p]); {vector<int>().swap(inorder);} {vector<int>().swap(preorder);} root->left=buildTree(leftpr,leftin); root->right=buildTree(rightpr,rightin); return root; }};
- Construct Binary Tree from Preorder and Inorder Traversal (中序和后序建树,在题目给的函数中完成)【leetcode】
- Construct Binary Tree from Inorder and Postorder Traversal(用中序和后序建树,在题目给定的函数中完成) 【leetcode】
- 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-先序和中序构造树
- Construct Binary Tree from Preorder and Inorder Traversal 从先序和中序重建二叉树@LeetCode
- 【LeetCode】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 根据先序和中序序列生成二叉树(重重)
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal(从前序遍历和中序遍历构造二叉树)
- 105.leetcode Construct Binary Tree from Preorder and Inorder Traversal(medium)[先序、中序构造二叉树]
- 【LeetCode笔记】Construct Binary Tree from Preorder and Inorder Traversal 前序、中序还原二叉树
- 105. Construct Binary Tree from Preorder and Inorder Traversal,前序+中序 构建 树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序重建二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序重建二叉树@LeetCode
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树
- 【LeetCode】Construct Binary Tree From Inorder And Postorder Traversal 中序遍历和后序遍历树构造二叉树
- mysql find_in_set方法在一个字段里匹配用','号分割的各个值(记录)
- (每日心得)WinForm下利用ADO.NET访问SQL Server数据库
- 浅谈 js中parseInt函数的解析
- 详解C#委托,事件与回调函数
- android 比较靠谱的图片压缩
- Construct Binary Tree from Preorder and Inorder Traversal (中序和后序建树,在题目给的函数中完成)【leetcode】
- linux解压tar命令
- js确认删除对话框
- 参加最多招聘会问题|贪心算法
- c++中数据的输入输出形式及数据形式的设置
- make的常见错误信息
- Linux下的ln命令
- android整合--FrameLayout和scrollview
- iOS NSString 转换为UTF-8编码