From f071df11f81f0090e611d8700d9868f38ecde55c Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 22 Nov 2022 00:01:41 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update=200106.=E4=BB=8E=E4=B8=AD=E5=BA=8F?= =?UTF-8?q?=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...序与后序遍历序列构造二叉树.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 17ba561d..0353ad62 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -1136,6 +1136,34 @@ object Solution { } ``` +## rust + +105 从前序与中序遍历序列构造二叉树 + +```rust +use std::cell::RefCell; +use std::rc::Rc; +impl Solution { + pub fn build_tree( + mut preorder: Vec, + mut inorder: Vec, + ) -> Option>> { + if preorder.is_empty() { + return None; + } + let root = preorder[0]; + let index = inorder.iter().position(|&x| x == root).unwrap(); + let mut root = TreeNode::new(root); + root.left = Self::build_tree( + preorder.splice(1..index + 1, []).collect(), + inorder.splice(0..index, []).collect(), + ); + root.right = Self::build_tree(preorder.split_off(1), inorder.split_off(1)); + Some(Rc::new(RefCell::new(root))) + } +} +``` +

From 3762f1a3e3e4f4f5ec1e3efc4d6ec9cf77079244 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Tue, 22 Nov 2022 00:18:08 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=200106.=E4=BB=8E=E4=B8=AD=E5=BA=8F?= =?UTF-8?q?=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...序与后序遍历序列构造二叉树.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 0353ad62..cf5f11d0 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -1138,6 +1138,30 @@ object Solution { ## rust +106 从中序与后序遍历序列构造二叉树 + +```rust +impl Solution { + pub fn build_tree( + mut inorder: Vec, + mut postorder: Vec, + ) -> Option>> { + if inorder.is_empty() { + return None; + } + let root = postorder.pop().unwrap(); + let index = inorder.iter().position(|&x| x == root).unwrap(); + let mut root = TreeNode::new(root); + root.left = Self::build_tree( + inorder.splice(0..index, []).collect(), + postorder.splice(0..index, []).collect(), + ); + root.right = Self::build_tree(inorder.split_off(1), postorder); + Some(Rc::new(RefCell::new(root))) + } +} +``` + 105 从前序与中序遍历序列构造二叉树 ```rust From 183a62a9237e0b2e3710985610421984400911e7 Mon Sep 17 00:00:00 2001 From: fw_qaq <82551626+Jack-Zhang-1314@users.noreply.github.com> Date: Wed, 23 Nov 2022 01:57:45 -0500 Subject: [PATCH 3/3] =?UTF-8?q?Update=200106.=E4=BB=8E=E4=B8=AD=E5=BA=8F?= =?UTF-8?q?=E4=B8=8E=E5=90=8E=E5=BA=8F=E9=81=8D=E5=8E=86=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E6=9E=84=E9=80=A0=E4=BA=8C=E5=8F=89=E6=A0=91.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...序与后序遍历序列构造二叉树.md | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index cf5f11d0..e5cbeda3 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -1141,22 +1141,19 @@ object Solution { 106 从中序与后序遍历序列构造二叉树 ```rust +use std::cell::RefCell; +use std::rc::Rc; impl Solution { - pub fn build_tree( - mut inorder: Vec, - mut postorder: Vec, - ) -> Option>> { + pub fn build_tree(inorder: Vec, postorder: Vec) -> Option>> { if inorder.is_empty() { return None; } + let mut postorder = postorder; let root = postorder.pop().unwrap(); let index = inorder.iter().position(|&x| x == root).unwrap(); let mut root = TreeNode::new(root); - root.left = Self::build_tree( - inorder.splice(0..index, []).collect(), - postorder.splice(0..index, []).collect(), - ); - root.right = Self::build_tree(inorder.split_off(1), postorder); + root.left = Self::build_tree(inorder[..index].to_vec(), postorder[..index].to_vec()); + root.right = Self::build_tree(inorder[index + 1..].to_vec(), postorder[index..].to_vec()); Some(Rc::new(RefCell::new(root))) } } @@ -1168,21 +1165,18 @@ impl Solution { use std::cell::RefCell; use std::rc::Rc; impl Solution { - pub fn build_tree( - mut preorder: Vec, - mut inorder: Vec, - ) -> Option>> { + pub fn build_tree(preorder: Vec, inorder: Vec) -> Option>> { if preorder.is_empty() { return None; } let root = preorder[0]; let index = inorder.iter().position(|&x| x == root).unwrap(); let mut root = TreeNode::new(root); - root.left = Self::build_tree( - preorder.splice(1..index + 1, []).collect(), - inorder.splice(0..index, []).collect(), + root.left = Self::build_tree(preorder[1..index + 1].to_vec(), inorder[0..index].to_vec()); + root.right = Self::build_tree( + preorder[index + 1..].to_vec(), + inorder[index + 1..].to_vec(), ); - root.right = Self::build_tree(preorder.split_off(1), inorder.split_off(1)); Some(Rc::new(RefCell::new(root))) } }