LeetCode No.106 Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:人工智能论文2000字 编辑:程序博客网 时间:2024/05/29 14:06
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
====================================================================================
题目链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
题目大意:根据二叉树的后序遍历和中序遍历构建二叉树。(二叉树中没有相同的数值)
思路:我们知道二叉树的后序遍历中最后个节点就是头结点
1、根据后序遍历中最后个节点值找出中序遍历的头结点
2、中序遍历头结点左边就是它的左子树(我们可以计算节点个数记为leftNum),头结点右边就是它的右子树。
3、根据leftNum,则后序遍历前面的leftNum个数就是它的左子树,除了最后一个节点外其他为右子树。
4、递归计算。
附上代码:
/** * 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) { return build ( inorder, 0 , inorder.size() - 1 , postorder , 0 , postorder.size() - 1 ) ; } TreeNode* build(vector<int>& inorder, int ii , int ij , vector<int>& postorder , int pi , int pj ) { if ( ij - ii < 0 ) return NULL ; int i = ii ;// i 记录分界点 for ( ; i <= ij ; i ++ ) if ( inorder[i] == postorder[pj] ) break ; int leftNum = i - ii ; TreeNode* root = new TreeNode ( postorder[pj] ) ; root -> left = build ( inorder , ii , i - 1 , postorder , pi , pi + leftNum - 1 ) ; root -> right = build ( inorder , i + 1 , ij , postorder , pi + leftNum , pj - 1 ) ; return root ; }};
0 0
- LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode(106)Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal [106]
- LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode 106] Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode 106]Construct Binary Tree from Inorder and Postorder Traversal
- leetcode || 106、Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal - LeetCode 106
- LeetCode 106:Construct Binary Tree from Postorder and Inorder Traversal
- LeetCode---(106)Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode #106 Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 106: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal
- 【LEETCODE】106-Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】【106】Construct Binary Tree from Inorder and Postorder Traversal
- 阿里巴巴2014秋招笔试题
- Leetcode 435. Non-overlapping Intervals题解
- shell脚本编程(三)——变量、常量和函数
- C++ 应用程序无法正常启动程序0xc00007b
- codevs 3955 最长严格上升子序列(加强版)(DP)
- LeetCode No.106 Construct Binary Tree from Inorder and Postorder Traversal
- Activity1
- 总结
- TCP 的那些事儿(下)
- JS 提交form表单
- Charles 从入门到精通-进阶篇
- JVM垃圾收集器总结
- 从第三方api获取行政区划市级以上数据
- VirtualPlanetBuilder编译出错处理