You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0900-0999/0965.Univalued Binary Tree/README_EN.md
+49-31
Original file line number
Diff line number
Diff line change
@@ -52,7 +52,15 @@ tags:
52
52
53
53
<!-- solution:start -->
54
54
55
-
### Solution 1
55
+
### Solution 1: DFS
56
+
57
+
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.
58
+
59
+
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}$.
60
+
61
+
In the main function, we call $\text{dfs}(\text{root})$ and return the result.
62
+
63
+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the tree.
0 commit comments