Skip to content
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

feat: add solutions to lc problem: No.1123 #4327

Merged
merged 1 commit into from
Apr 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,22 @@ class Solution {
class Solution {
public:
TreeNode* lcaDeepestLeaves(TreeNode* root) {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair<TreeNode*, int> {
if (!root) {
return {nullptr, 0};
}
auto [l, d1] = dfs(root->left);
auto [r, d2] = dfs(root->right);
if (d1 > d2) {
return {l, d1 + 1};
}
if (d1 < d2) {
return {r, d2 + 1};
}
return {root, d1 + 1};
};
return dfs(root).first;
}

pair<TreeNode*, int> dfs(TreeNode* root) {
if (!root) {
return {nullptr, 0};
}
auto [l, d1] = dfs(root->left);
auto [r, d2] = dfs(root->right);
if (d1 > d2) {
return {l, d1 + 1};
}
if (d1 < d2) {
return {r, d2 + 1};
}
return {root, d1 + 1};
}
};
```

Expand Down Expand Up @@ -267,6 +266,48 @@ function lcaDeepestLeaves(root: TreeNode | null): TreeNode | null {
}
```

#### C#

```cs
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode LcaDeepestLeaves(TreeNode root) {
(TreeNode, int) Dfs(TreeNode root) {
if (root == null) {
return (null, 0);
}

var l = Dfs(root.left);
var r = Dfs(root.right);
int d1 = l.Item2;
int d2 = r.Item2;

if (d1 > d2) {
return (l.Item1, d1 + 1);
}
if (d1 < d2) {
return (r.Item1, d2 + 1);
}
return (root, d1 + 1);
}

return Dfs(root).Item1;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,22 @@ class Solution {
class Solution {
public:
TreeNode* lcaDeepestLeaves(TreeNode* root) {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair<TreeNode*, int> {
if (!root) {
return {nullptr, 0};
}
auto [l, d1] = dfs(root->left);
auto [r, d2] = dfs(root->right);
if (d1 > d2) {
return {l, d1 + 1};
}
if (d1 < d2) {
return {r, d2 + 1};
}
return {root, d1 + 1};
};
return dfs(root).first;
}

pair<TreeNode*, int> dfs(TreeNode* root) {
if (!root) {
return {nullptr, 0};
}
auto [l, d1] = dfs(root->left);
auto [r, d2] = dfs(root->right);
if (d1 > d2) {
return {l, d1 + 1};
}
if (d1 < d2) {
return {r, d2 + 1};
}
return {root, d1 + 1};
}
};
```

Expand Down Expand Up @@ -264,6 +263,48 @@ function lcaDeepestLeaves(root: TreeNode | null): TreeNode | null {
}
```

#### C#

```cs
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode LcaDeepestLeaves(TreeNode root) {
(TreeNode, int) Dfs(TreeNode root) {
if (root == null) {
return (null, 0);
}

var l = Dfs(root.left);
var r = Dfs(root.right);
int d1 = l.Item2;
int d2 = r.Item2;

if (d1 > d2) {
return (l.Item1, d1 + 1);
}
if (d1 < d2) {
return (r.Item1, d2 + 1);
}
return (root, d1 + 1);
}

return Dfs(root).Item1;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@
class Solution {
public:
TreeNode* lcaDeepestLeaves(TreeNode* root) {
auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair<TreeNode*, int> {
if (!root) {
return {nullptr, 0};
}
auto [l, d1] = dfs(root->left);
auto [r, d2] = dfs(root->right);
if (d1 > d2) {
return {l, d1 + 1};
}
if (d1 < d2) {
return {r, d2 + 1};
}
return {root, d1 + 1};
};
return dfs(root).first;
}

pair<TreeNode*, int> dfs(TreeNode* root) {
if (!root) {
return {nullptr, 0};
}
auto [l, d1] = dfs(root->left);
auto [r, d2] = dfs(root->right);
if (d1 > d2) {
return {l, d1 + 1};
}
if (d1 < d2) {
return {r, d2 + 1};
}
return {root, d1 + 1};
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public TreeNode LcaDeepestLeaves(TreeNode root) {
(TreeNode, int) Dfs(TreeNode root) {
if (root == null) {
return (null, 0);
}

var l = Dfs(root.left);
var r = Dfs(root.right);
int d1 = l.Item2;
int d2 = r.Item2;

if (d1 > d2) {
return (l.Item1, d1 + 1);
}
if (d1 < d2) {
return (r.Item1, d2 + 1);
}
return (root, d1 + 1);
}

return Dfs(root).Item1;
}
}