【LeetCode】C# 106、Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:乐视视频有mac版吗 编辑:程序博客网 时间:2024/06/10 11:25
Given inorder and postorder 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. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode BuildTree(int[] inorder, int[] postorder) { if (inorder == null || postorder == null || inorder.Length == 0 || postorder.Length == 0) { return null; } Dictionary<int, int> map = new Dictionary<int, int>(); for (int i = 0; i < inorder.Length; i++) { map.Add(inorder[i], i); } return helper(inorder, postorder, 0, inorder.Length - 1, 0, postorder.Length - 1, map); } private TreeNode helper(int[] inorder, int[] postorder, int inL, int inR, int postL, int postR, Dictionary<int, int> map) { if (inL > inR || postL > postR) return null; TreeNode root = new TreeNode(postorder[postR]); int index = map[root.val]; root.left = helper(inorder, postorder, inL, index - 1, postL, postL + index - inL - 1, map); root.right = helper(inorder, postorder, index + 1, inR, postR - (inR - index), postR - 1, map); 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
- leetcode解题方案--015--3 sum
- jquery常用事件
- Deprecated</b>: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a
- SQL语句中表的基本操作
- 电脑中的声音
- 【LeetCode】C# 106、Construct Binary Tree from Inorder and Postorder Traversal
- 直接选择排序
- Vision_数据结构_线段树
- python切片
- URI中 ? # & 的作用
- xadmin下设置“use_bootswatch = True”无效的原因
- TP框架中的M,D,C,A,I,S方法
- 关于a ++ 与++ a的问题
- 【Leetcode-Easy-155】 Min Stack