105. Construct Binary Tree from Preorder and Inorder Traversal\106. Construct Binary Tree from Inor
来源:互联网 发布:java类加载器有哪些 编辑:程序博客网 时间:2024/06/06 13:00
- Construct Binary Tree from Preorder and Inorder Traversal
- 题目描述
- 代码实现
- Construct Binary Tree from Inorder and Postorder Traversal
- 题目描述
- 代码实现
105. Construct Binary Tree from Preorder and Inorder Traversal
题目描述
从二叉树的前序和中序遍历中恢复原来的二叉树。
从先序遍历中可以很轻松的找到根节点,从中序遍历中可以把左右子树分割出来。
所以这道题目使用DFS再合适不过了。
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 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* buildTree(vector<int> preorder, vector<int> inorder) { int insize = inorder.size(); if(!insize) return NULL; TreeNode* root = new TreeNode(preorder[0]); int i = 0; for(i = 0; i < insize; i++) if(inorder[i] == preorder[0]) break; if(i) root->left = buildTree(vector<int>(preorder.begin()+1, preorder.begin()+i+1), vector<int>(inorder.begin(), inorder.begin()+i)); else root->left = NULL; if(i+1 < insize) { root->right = buildTree(vector<int>(preorder.begin() + i + 1, preorder.end()), vector<int>(inorder.begin()+i+1, inorder.end())); } else root->right = NULL; return root; }};
106. Construct Binary Tree from Inorder and Postorder Traversal
题目描述
给中序和后序,给出二叉树。
Given inorder and postorder 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* buildTree(vector<int> inorder, vector<int> postorder) { int len = inorder.size(), ind = -1; if(!len) return NULL; TreeNode *root = new TreeNode(postorder[len-1]); for(ind = 0; ind < len; ind++) if(inorder[ind] == postorder[len - 1]) break; root->left = ind?buildTree(vector<int>(inorder.begin(), inorder.begin()+ind), vector<int>(postorder.begin(), postorder.begin()+ind)):NULL; root->right = ind+1<len?buildTree(vector<int>(inorder.begin()+ind+1, inorder.end()), vector<int>(postorder.begin()+ind, postorder.begin()+len)):NULL; return root; }};
0 0
- 105. Construct Binary Tree from Preorder and Inorder Traversal\106. Construct Binary Tree from Inor
- LeetCode 105. 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]*105.Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode] 105.Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. 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
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 367. Valid Perfect Square
- 2016蓝桥杯第二题答案
- BZOJ 3729 splay维护DFS序+博弈论
- 一位资深程序员大牛给予Java初学者的学习建议
- SVM支持向量机
- 105. Construct Binary Tree from Preorder and Inorder Traversal\106. Construct Binary Tree from Inor
- 当FragmentTransaction在add和replace时,它们之间的区别
- 物联网操作系统HelloX V1.81发布公告
- 原型模式
- IPC机制--概述
- Flume 1.7 源码分析(一)源码编译
- IJ IDEA创建Spring Boot项目初始化失败-无法下载
- 《自顶向下法》学习笔记——第四章:网络层
- nosql和关系型数据库比较