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/1400-1499/1469.Find All The Lonely Nodes/README_EN.md
+66-16
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,16 @@ All other nodes are lonely.
67
67
68
68
<!-- solution:start -->
69
69
70
-
### Solution 1
70
+
### Solution 1: DFS
71
+
72
+
We can use Depth-First Search (DFS) to traverse the entire tree. We design a function $\textit{dfs}$, which traverses each node in the tree. If the current node is a lone child, we add its value to the answer array. The execution process of the function $\textit{dfs}$ is as follows:
73
+
74
+
1. If the current node is null, or the current node is a leaf node (i.e., both the left and right children of the current node are null), then return directly.
75
+
2. If the left child of the current node is null, then the right child of the current node is a lone child, and we add its value to the answer array.
76
+
3. If the right child of the current node is null, then the left child of the current node is a lone child, and we add its value to the answer array.
77
+
4. Recursively traverse the left and right children of the current node.
78
+
79
+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
0 commit comments