From 734616dff0402cf35029835fcf71c1ce2527509e Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Fri, 24 Nov 2023 19:01:11 +0800 Subject: [PATCH] =?UTF-8?q?Update=200106.=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...从中序与后序遍历序列构造二叉树.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 0144fc5c..dc517480 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -1228,6 +1228,19 @@ impl Solution { } } ``` +### C# +```C# +public TreeNode BuildTree(int[] inorder, int[] postorder) +{ + if (inorder.Length == 0 || postorder.Length == null) return null; + int rootValue = postorder.Last(); + TreeNode root = new TreeNode(rootValue); + int delimiterIndex = Array.IndexOf(inorder, rootValue); + root.left = BuildTree(inorder.Take(delimiterIndex).ToArray(), postorder.Take(delimiterIndex).ToArray()); + root.right = BuildTree(inorder.Skip(delimiterIndex + 1).ToArray(), postorder.Skip(delimiterIndex).Take(inorder.Length - delimiterIndex - 1).ToArray()); + return root; +} +```