LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:ubuntu wget安装 编辑:程序博客网 时间:2024/06/03 21:25
Construct Binary Tree from Preorder and Inorder Traversal
题目描述:
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) { return build(preorder, inorder, 0, preorder.size()-1, 0, inorder.size()-1); } TreeNode* build(vector<int>& preorder, vector<int>& inorder, int ps, int pe, int is, int ie){ if(ps > pe || is > ie) return nullptr; TreeNode* root = new TreeNode(preorder[ps]); int i = 0; while(inorder[i] != root->val) i++; root->left = build(preorder, inorder, ps+1, ps+i-is, is, i-1); root->right = build(preorder, inorder, ps+i-is+1, pe,i+1, ie); return root; } };
阅读全文
1 0
- 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
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal LeetCode
- 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
- 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
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
- Myeclipse2015把web项目添加至tomcat中,添加列表中却没有想要添加的项目
- 《Springboot极简教程》问题解决:Springboot启动报错 Whitelabel Error Page: This application has no explicit mapping for
- 《Springboot极简教程》问题解决:javax.servlet.ServletException: Circular view path [login]: would dispatch back to the current handler URL
- 原声JS判断程序运行平台Pc OR Mobile
- 《Springboot极简教程》继承WebMvcConfigurerAdapter: 一行代码写Controller
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 搭建Maven(搭建+问题笔记)
- 《Springboot极简教程》问题解决:Idea解决Gradle库依赖冲突问题:Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting
- 传递WinMain的参数给main
- 《Springboot极简教程》问题解决:org.apache.catalina.LifecycleException: A child container failed during start
- 邮件发送和接收原理
- 【tyvj1098】任务安排(dp)
- 《Springboot极简教程》Springboot使用Kotlin和Java混合编程
- Elam的caffe笔记之配置篇(六):Centos6.5下编译caffe及caffe的python3.6接口