diff --git a/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README.md b/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README.md index 29092606f1..bb15a9cc13 100644 --- a/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README.md +++ b/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README.md @@ -169,6 +169,81 @@ func sumRootToLeaf(root *TreeNode) int { } ``` +### **TypeScript** + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function sumRootToLeaf(root: TreeNode | null): number { + const dfs = (root: TreeNode | null, num: number) => { + if (root == null) { + return 0; + } + const { val, left, right } = root; + num = (num << 1) | val; + if (left == null && right == null) { + return num; + } + return dfs(left, num) + dfs(right, num); + }; + return dfs(root, 0); +} +``` + +### **Rust** + +```rust +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + fn dfs(root: &Option>>, mut num: i32) -> i32 { + if root.is_none() { + return 0; + } + let root = root.as_ref().unwrap().borrow(); + num = (num << 1) | root.val; + if root.left.is_none() && root.right.is_none() { + return num; + } + Self::dfs(&root.left, num) + Self::dfs(&root.right, num) + } + + pub fn sum_root_to_leaf(root: Option>>) -> i32 { + Self::dfs(&root, 0) + } +} +``` + ### **...** ``` diff --git a/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README_EN.md b/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README_EN.md index 4bf61b9062..6fab83e1a8 100644 --- a/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README_EN.md +++ b/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README_EN.md @@ -159,6 +159,81 @@ func sumRootToLeaf(root *TreeNode) int { } ``` +### **TypeScript** + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function sumRootToLeaf(root: TreeNode | null): number { + const dfs = (root: TreeNode | null, num: number) => { + if (root == null) { + return 0; + } + const { val, left, right } = root; + num = (num << 1) | val; + if (left == null && right == null) { + return num; + } + return dfs(left, num) + dfs(right, num); + }; + return dfs(root, 0); +} +``` + +### **Rust** + +```rust +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + fn dfs(root: &Option>>, mut num: i32) -> i32 { + if root.is_none() { + return 0; + } + let root = root.as_ref().unwrap().borrow(); + num = (num << 1) | root.val; + if root.left.is_none() && root.right.is_none() { + return num; + } + Self::dfs(&root.left, num) + Self::dfs(&root.right, num) + } + + pub fn sum_root_to_leaf(root: Option>>) -> i32 { + Self::dfs(&root, 0) + } +} +``` + ### **...** ``` diff --git a/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/Solution.rs b/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/Solution.rs new file mode 100644 index 0000000000..46d412bfff --- /dev/null +++ b/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/Solution.rs @@ -0,0 +1,37 @@ +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + fn dfs(root: &Option>>, mut num: i32) -> i32 { + if root.is_none() { + return 0; + } + let root = root.as_ref().unwrap().borrow(); + num = (num << 1) | root.val; + if root.left.is_none() && root.right.is_none() { + return num; + } + Self::dfs(&root.left, num) + Self::dfs(&root.right, num) + } + + pub fn sum_root_to_leaf(root: Option>>) -> i32 { + Self::dfs(&root, 0) + } +} diff --git a/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/Solution.ts b/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/Solution.ts new file mode 100644 index 0000000000..ea69e5a211 --- /dev/null +++ b/solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/Solution.ts @@ -0,0 +1,28 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function sumRootToLeaf(root: TreeNode | null): number { + const dfs = (root: TreeNode | null, num: number) => { + if (root == null) { + return 0; + } + const { val, left, right } = root; + num = (num << 1) | val; + if (left == null && right == null) { + return num; + } + return dfs(left, num) + dfs(right, num); + }; + return dfs(root, 0); +}