Skip to content

feat: add solutions to lc problems: No.0965,0978 #3954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 49 additions & 31 deletions solution/0900-0999/0965.Univalued Binary Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:DFS

我们记根节点的值为 $x$,然后设计一个函数 $\text{dfs}(\text{root})$,它表示当前节点的值是否等于 $x$,并且它的左右子树也是单值二叉树。

在函数 $\text{dfs}(\text{root})$ 中,如果当前节点为空,那么返回 $\text{true}$,否则,如果当前节点的值等于 $x$,并且它的左右子树也是单值二叉树,那么返回 $\text{true}$,否则返回 $\text{false}$。

在主函数中,我们调用 $\text{dfs}(\text{root})$,并返回结果。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树中的节点数目。

<!-- tabs:start -->

Expand All @@ -70,12 +78,13 @@ tags:
# self.left = left
# self.right = right
class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
def dfs(node):
if node is None:
def isUnivalTree(self, root: Optional[TreeNode]) -> bool:
def dfs(root: Optional[TreeNode]) -> bool:
if root is None:
return True
return node.val == root.val and dfs(node.left) and dfs(node.right)
return root.val == x and dfs(root.left) and dfs(root.right)

x = root.val
return dfs(root)
```

Expand All @@ -98,15 +107,18 @@ class Solution:
* }
*/
class Solution {
private int x;

public boolean isUnivalTree(TreeNode root) {
return dfs(root, root.val);
x = root.val;
return dfs(root);
}

private boolean dfs(TreeNode root, int val) {
private boolean dfs(TreeNode root) {
if (root == null) {
return true;
}
return root.val == val && dfs(root.left, val) && dfs(root.right, val);
return root.val == x && dfs(root.left) && dfs(root.right);
}
}
```
Expand All @@ -128,12 +140,14 @@ class Solution {
class Solution {
public:
bool isUnivalTree(TreeNode* root) {
return dfs(root, root->val);
}

bool dfs(TreeNode* root, int val) {
if (!root) return true;
return root->val == val && dfs(root->left, val) && dfs(root->right, val);
int x = root->val;
auto dfs = [&](this auto&& dfs, TreeNode* root) -> bool {
if (!root) {
return true;
}
return root->val == x && dfs(root->left) && dfs(root->right);
};
return dfs(root);
}
};
```
Expand All @@ -150,12 +164,13 @@ public:
* }
*/
func isUnivalTree(root *TreeNode) bool {
x := root.Val
var dfs func(*TreeNode) bool
dfs = func(node *TreeNode) bool {
if node == nil {
dfs = func(root *TreeNode) bool {
if root == nil {
return true
}
return node.Val == root.Val && dfs(node.Left) && dfs(node.Right)
return root.Val == x && dfs(root.Left) && dfs(root.Right)
}
return dfs(root)
}
Expand All @@ -179,14 +194,14 @@ func isUnivalTree(root *TreeNode) bool {
*/

function isUnivalTree(root: TreeNode | null): boolean {
const val = root.val;
const dfs = (root: TreeNode | null) => {
if (root == null) {
const x = root!.val;
const dfs = (root: TreeNode | null): boolean => {
if (!root) {
return true;
}
return root.val === val && dfs(root.left) && dfs(root.right);
return root.val === x && dfs(root.left) && dfs(root.right);
};
return dfs(root.left) && dfs(root.right);
return dfs(root);
}
```

Expand Down Expand Up @@ -214,16 +229,19 @@ function isUnivalTree(root: TreeNode | null): boolean {
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn dfs(val: i32, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
if root.is_none() {
return true;
}
let root = root.as_ref().unwrap().borrow();
root.val == val && Self::dfs(val, &root.left) && Self::dfs(val, &root.right)
}
pub fn is_unival_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
let root = root.as_ref().unwrap().borrow();
Self::dfs(root.val, &root.left) && Self::dfs(root.val, &root.right)
let x = root.as_ref().unwrap().borrow().val;

fn dfs(node: Option<Rc<RefCell<TreeNode>>>, x: i32) -> bool {
if let Some(n) = node {
let n = n.borrow();
n.val == x && dfs(n.left.clone(), x) && dfs(n.right.clone(), x)
} else {
true
}
}

dfs(root, x)
}
}
```
Expand Down
80 changes: 49 additions & 31 deletions solution/0900-0999/0965.Univalued Binary Tree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: DFS

We denote the value of the root node as $x$, and then design a function $\text{dfs}(\text{root})$, which indicates whether the current node's value is equal to $x$ and its left and right subtrees are also univalued binary trees.

In the function $\text{dfs}(\text{root})$, if the current node is null, return $\text{true}$; otherwise, if the current node's value is equal to $x$ and its left and right subtrees are also univalued binary trees, return $\text{true}$; otherwise, return $\text{false}$.

In the main function, we call $\text{dfs}(\text{root})$ and return the result.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the tree.

<!-- tabs:start -->

Expand All @@ -66,12 +74,13 @@ tags:
# self.left = left
# self.right = right
class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
def dfs(node):
if node is None:
def isUnivalTree(self, root: Optional[TreeNode]) -> bool:
def dfs(root: Optional[TreeNode]) -> bool:
if root is None:
return True
return node.val == root.val and dfs(node.left) and dfs(node.right)
return root.val == x and dfs(root.left) and dfs(root.right)

x = root.val
return dfs(root)
```

Expand All @@ -94,15 +103,18 @@ class Solution:
* }
*/
class Solution {
private int x;

public boolean isUnivalTree(TreeNode root) {
return dfs(root, root.val);
x = root.val;
return dfs(root);
}

private boolean dfs(TreeNode root, int val) {
private boolean dfs(TreeNode root) {
if (root == null) {
return true;
}
return root.val == val && dfs(root.left, val) && dfs(root.right, val);
return root.val == x && dfs(root.left) && dfs(root.right);
}
}
```
Expand All @@ -124,12 +136,14 @@ class Solution {
class Solution {
public:
bool isUnivalTree(TreeNode* root) {
return dfs(root, root->val);
}

bool dfs(TreeNode* root, int val) {
if (!root) return true;
return root->val == val && dfs(root->left, val) && dfs(root->right, val);
int x = root->val;
auto dfs = [&](this auto&& dfs, TreeNode* root) -> bool {
if (!root) {
return true;
}
return root->val == x && dfs(root->left) && dfs(root->right);
};
return dfs(root);
}
};
```
Expand All @@ -146,12 +160,13 @@ public:
* }
*/
func isUnivalTree(root *TreeNode) bool {
x := root.Val
var dfs func(*TreeNode) bool
dfs = func(node *TreeNode) bool {
if node == nil {
dfs = func(root *TreeNode) bool {
if root == nil {
return true
}
return node.Val == root.Val && dfs(node.Left) && dfs(node.Right)
return root.Val == x && dfs(root.Left) && dfs(root.Right)
}
return dfs(root)
}
Expand All @@ -175,14 +190,14 @@ func isUnivalTree(root *TreeNode) bool {
*/

function isUnivalTree(root: TreeNode | null): boolean {
const val = root.val;
const dfs = (root: TreeNode | null) => {
if (root == null) {
const x = root!.val;
const dfs = (root: TreeNode | null): boolean => {
if (!root) {
return true;
}
return root.val === val && dfs(root.left) && dfs(root.right);
return root.val === x && dfs(root.left) && dfs(root.right);
};
return dfs(root.left) && dfs(root.right);
return dfs(root);
}
```

Expand Down Expand Up @@ -210,16 +225,19 @@ function isUnivalTree(root: TreeNode | null): boolean {
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn dfs(val: i32, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
if root.is_none() {
return true;
}
let root = root.as_ref().unwrap().borrow();
root.val == val && Self::dfs(val, &root.left) && Self::dfs(val, &root.right)
}
pub fn is_unival_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
let root = root.as_ref().unwrap().borrow();
Self::dfs(root.val, &root.left) && Self::dfs(root.val, &root.right)
let x = root.as_ref().unwrap().borrow().val;

fn dfs(node: Option<Rc<RefCell<TreeNode>>>, x: i32) -> bool {
if let Some(n) = node {
let n = n.borrow();
n.val == x && dfs(n.left.clone(), x) && dfs(n.right.clone(), x)
} else {
true
}
}

dfs(root, x)
}
}
```
Expand Down
16 changes: 9 additions & 7 deletions solution/0900-0999/0965.Univalued Binary Tree/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
class Solution {
public:
bool isUnivalTree(TreeNode* root) {
return dfs(root, root->val);
int x = root->val;
auto dfs = [&](this auto&& dfs, TreeNode* root) -> bool {
if (!root) {
return true;
}
return root->val == x && dfs(root->left) && dfs(root->right);
};
return dfs(root);
}

bool dfs(TreeNode* root, int val) {
if (!root) return true;
return root->val == val && dfs(root->left, val) && dfs(root->right, val);
}
};
};
9 changes: 5 additions & 4 deletions solution/0900-0999/0965.Univalued Binary Tree/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
* }
*/
func isUnivalTree(root *TreeNode) bool {
x := root.Val
var dfs func(*TreeNode) bool
dfs = func(node *TreeNode) bool {
if node == nil {
dfs = func(root *TreeNode) bool {
if root == nil {
return true
}
return node.Val == root.Val && dfs(node.Left) && dfs(node.Right)
return root.Val == x && dfs(root.Left) && dfs(root.Right)
}
return dfs(root)
}
}
11 changes: 7 additions & 4 deletions solution/0900-0999/0965.Univalued Binary Tree/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
* }
*/
class Solution {
private int x;

public boolean isUnivalTree(TreeNode root) {
return dfs(root, root.val);
x = root.val;
return dfs(root);
}

private boolean dfs(TreeNode root, int val) {
private boolean dfs(TreeNode root) {
if (root == null) {
return true;
}
return root.val == val && dfs(root.left, val) && dfs(root.right, val);
return root.val == x && dfs(root.left) && dfs(root.right);
}
}
}
9 changes: 5 additions & 4 deletions solution/0900-0999/0965.Univalued Binary Tree/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
# self.left = left
# self.right = right
class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
def dfs(node):
if node is None:
def isUnivalTree(self, root: Optional[TreeNode]) -> bool:
def dfs(root: Optional[TreeNode]) -> bool:
if root is None:
return True
return node.val == root.val and dfs(node.left) and dfs(node.right)
return root.val == x and dfs(root.left) and dfs(root.right)

x = root.val
return dfs(root)
21 changes: 12 additions & 9 deletions solution/0900-0999/0965.Univalued Binary Tree/Solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn dfs(val: i32, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
if root.is_none() {
return true;
}
let root = root.as_ref().unwrap().borrow();
root.val == val && Self::dfs(val, &root.left) && Self::dfs(val, &root.right)
}
pub fn is_unival_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
let root = root.as_ref().unwrap().borrow();
Self::dfs(root.val, &root.left) && Self::dfs(root.val, &root.right)
let x = root.as_ref().unwrap().borrow().val;

fn dfs(node: Option<Rc<RefCell<TreeNode>>>, x: i32) -> bool {
if let Some(n) = node {
let n = n.borrow();
n.val == x && dfs(n.left.clone(), x) && dfs(n.right.clone(), x)
} else {
true
}
}

dfs(root, x)
}
}
Loading
Loading