mirror of https://github.com/doocs/leetcode.git
feat: add solutions to lc problem: No.1022
No.1022.Sum of Root To Leaf Binary Numbers
This commit is contained in:
parent
ddd6a958ef
commit
ba518558c7
|
|
@ -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<Rc<RefCell<TreeNode>>>,
|
||||
// pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
// }
|
||||
//
|
||||
// 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<Rc<RefCell<TreeNode>>>, 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<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
Self::dfs(&root, 0)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -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<Rc<RefCell<TreeNode>>>,
|
||||
// pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
// }
|
||||
//
|
||||
// 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<Rc<RefCell<TreeNode>>>, 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<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
Self::dfs(&root, 0)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **...**
|
||||
|
||||
```
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
// Definition for a binary tree node.
|
||||
// #[derive(Debug, PartialEq, Eq)]
|
||||
// pub struct TreeNode {
|
||||
// pub val: i32,
|
||||
// pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
// pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
// }
|
||||
//
|
||||
// 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<Rc<RefCell<TreeNode>>>, 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<Rc<RefCell<TreeNode>>>) -> i32 {
|
||||
Self::dfs(&root, 0)
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
Loading…
Reference in New Issue