Skip to content
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
82 changes: 64 additions & 18 deletions solution/1400-1499/1469.Find All The Lonely Nodes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ tags:

<!-- solution:start -->

### 方法一:递归
### 方法一:DFS

递归搜索二叉树,如果当前节点的左右子节点都不为空,则继续递归搜索左右子树;如果当前节点的左右子节点有一个为空,则将不为空的子节点的值加入结果数组中,然后继续递归搜索左右子树。
我们可以使用深度优先搜索遍历整棵树,设计一个函数 $\textit{dfs}$,它的作用是遍历树中的每个节点,如果当前节点是独生节点,那么将其值加入答案数组中。函数 $\textit{dfs}$ 的执行过程如下:

时间复杂度 $O(n)$,其中 $n$ 为二叉树的节点个数。需要对二叉树进行一次遍历。
1. 如果当前节点为空,或者当前节点是叶子节点,即当前节点的左右子节点都为空,那么直接返回。
2. 如果当前节点的左子节点为空,那么将当前节点的右子节点是独生节点,将其值加入答案数组中。
3. 如果当前节点的右子节点为空,那么将当前节点的左子节点是独生节点,将其值加入答案数组中。
4. 递归遍历当前节点的左子节点和右子节点。

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

<!-- tabs:start -->

Expand All @@ -93,8 +98,8 @@ tags:
# self.right = right
class Solution:
def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]:
def dfs(root):
if root is None or (root.left is None and root.right is None):
def dfs(root: Optional[TreeNode]):
if root is None or root.left == root.right:
return
if root.left is None:
ans.append(root.right.val)
Expand Down Expand Up @@ -135,7 +140,7 @@ class Solution {
}

private void dfs(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) {
if (root == null || (root.left == root.right)) {
return;
}
if (root.left == null) {
Expand Down Expand Up @@ -168,15 +173,20 @@ class Solution {
public:
vector<int> getLonelyNodes(TreeNode* root) {
vector<int> ans;
function<void(TreeNode * root)> dfs;
dfs = [&](TreeNode* root) {
if (!root || (!root->left && !root->right)) return;
if (!root->left) ans.push_back(root->right->val);
if (!root->right) ans.push_back(root->left->val);
dfs(root->left);
dfs(root->right);
auto dfs = [&](auto&& dfs, TreeNode* root) {
if (!root || (root->left == root->right)) {
return;
}
if (!root->left) {
ans.push_back(root->right->val);
}
if (!root->right) {
ans.push_back(root->left->val);
}
dfs(dfs, root->left);
dfs(dfs, root->right);
};
dfs(root);
dfs(dfs, root);
return ans;
}
};
Expand All @@ -193,11 +203,10 @@ public:
* Right *TreeNode
* }
*/
func getLonelyNodes(root *TreeNode) []int {
ans := []int{}
func getLonelyNodes(root *TreeNode) (ans []int) {
var dfs func(*TreeNode)
dfs = func(root *TreeNode) {
if root == nil || (root.Left == nil && root.Right == nil) {
if root == nil || (root.Left == root.Right) {
return
}
if root.Left == nil {
Expand All @@ -210,7 +219,44 @@ func getLonelyNodes(root *TreeNode) []int {
dfs(root.Right)
}
dfs(root)
return ans
return
}
```

#### 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 getLonelyNodes(root: TreeNode | null): number[] {
const ans: number[] = [];
const dfs = (root: TreeNode | null) => {
if (!root || root.left === root.right) {
return;
}
if (!root.left) {
ans.push(root.right.val);
}
if (!root.right) {
ans.push(root.left.val);
}
dfs(root.left);
dfs(root.right);
};
dfs(root);
return ans;
}
```

Expand Down
82 changes: 66 additions & 16 deletions solution/1400-1499/1469.Find All The Lonely Nodes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ All other nodes are lonely.

<!-- solution:start -->

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

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:

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.
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.
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.
4. Recursively traverse the left and right children of the current node.

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

<!-- tabs:start -->

Expand All @@ -82,8 +91,8 @@ All other nodes are lonely.
# self.right = right
class Solution:
def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]:
def dfs(root):
if root is None or (root.left is None and root.right is None):
def dfs(root: Optional[TreeNode]):
if root is None or root.left == root.right:
return
if root.left is None:
ans.append(root.right.val)
Expand Down Expand Up @@ -124,7 +133,7 @@ class Solution {
}

private void dfs(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) {
if (root == null || (root.left == root.right)) {
return;
}
if (root.left == null) {
Expand Down Expand Up @@ -157,15 +166,20 @@ class Solution {
public:
vector<int> getLonelyNodes(TreeNode* root) {
vector<int> ans;
function<void(TreeNode * root)> dfs;
dfs = [&](TreeNode* root) {
if (!root || (!root->left && !root->right)) return;
if (!root->left) ans.push_back(root->right->val);
if (!root->right) ans.push_back(root->left->val);
dfs(root->left);
dfs(root->right);
auto dfs = [&](auto&& dfs, TreeNode* root) {
if (!root || (root->left == root->right)) {
return;
}
if (!root->left) {
ans.push_back(root->right->val);
}
if (!root->right) {
ans.push_back(root->left->val);
}
dfs(dfs, root->left);
dfs(dfs, root->right);
};
dfs(root);
dfs(dfs, root);
return ans;
}
};
Expand All @@ -182,11 +196,10 @@ public:
* Right *TreeNode
* }
*/
func getLonelyNodes(root *TreeNode) []int {
ans := []int{}
func getLonelyNodes(root *TreeNode) (ans []int) {
var dfs func(*TreeNode)
dfs = func(root *TreeNode) {
if root == nil || (root.Left == nil && root.Right == nil) {
if root == nil || (root.Left == root.Right) {
return
}
if root.Left == nil {
Expand All @@ -199,7 +212,44 @@ func getLonelyNodes(root *TreeNode) []int {
dfs(root.Right)
}
dfs(root)
return ans
return
}
```

#### 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 getLonelyNodes(root: TreeNode | null): number[] {
const ans: number[] = [];
const dfs = (root: TreeNode | null) => {
if (!root || root.left === root.right) {
return;
}
if (!root.left) {
ans.push(root.right.val);
}
if (!root.right) {
ans.push(root.left.val);
}
dfs(root.left);
dfs(root.right);
};
dfs(root);
return ans;
}
```

Expand Down
23 changes: 14 additions & 9 deletions solution/1400-1499/1469.Find All The Lonely Nodes/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ class Solution {
public:
vector<int> getLonelyNodes(TreeNode* root) {
vector<int> ans;
function<void(TreeNode * root)> dfs;
dfs = [&](TreeNode* root) {
if (!root || (!root->left && !root->right)) return;
if (!root->left) ans.push_back(root->right->val);
if (!root->right) ans.push_back(root->left->val);
dfs(root->left);
dfs(root->right);
auto dfs = [&](auto&& dfs, TreeNode* root) {
if (!root || (root->left == root->right)) {
return;
}
if (!root->left) {
ans.push_back(root->right->val);
}
if (!root->right) {
ans.push_back(root->left->val);
}
dfs(dfs, root->left);
dfs(dfs, root->right);
};
dfs(root);
dfs(dfs, root);
return ans;
}
};
};
9 changes: 4 additions & 5 deletions solution/1400-1499/1469.Find All The Lonely Nodes/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
* Right *TreeNode
* }
*/
func getLonelyNodes(root *TreeNode) []int {
ans := []int{}
func getLonelyNodes(root *TreeNode) (ans []int) {
var dfs func(*TreeNode)
dfs = func(root *TreeNode) {
if root == nil || (root.Left == nil && root.Right == nil) {
if root == nil || (root.Left == root.Right) {
return
}
if root.Left == nil {
Expand All @@ -23,5 +22,5 @@ func getLonelyNodes(root *TreeNode) []int {
dfs(root.Right)
}
dfs(root)
return ans
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public List<Integer> getLonelyNodes(TreeNode root) {
}

private void dfs(TreeNode root) {
if (root == null || (root.left == null && root.right == null)) {
if (root == null || (root.left == root.right)) {
return;
}
if (root.left == null) {
Expand All @@ -34,4 +34,4 @@ private void dfs(TreeNode root) {
dfs(root.left);
dfs(root.right);
}
}
}
4 changes: 2 additions & 2 deletions solution/1400-1499/1469.Find All The Lonely Nodes/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
# self.right = right
class Solution:
def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]:
def dfs(root):
if root is None or (root.left is None and root.right is None):
def dfs(root: Optional[TreeNode]):
if root is None or root.left == root.right:
return
if root.left is None:
ans.append(root.right.val)
Expand Down
32 changes: 32 additions & 0 deletions solution/1400-1499/1469.Find All The Lonely Nodes/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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 getLonelyNodes(root: TreeNode | null): number[] {
const ans: number[] = [];
const dfs = (root: TreeNode | null) => {
if (!root || root.left === root.right) {
return;
}
if (!root.left) {
ans.push(root.right.val);
}
if (!root.right) {
ans.push(root.left.val);
}
dfs(root.left);
dfs(root.right);
};
dfs(root);
return ans;
}
Loading
Loading