diff --git a/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/README.md b/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/README.md index fc17eb98f7b14..f349c76c2d688 100644 --- a/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/README.md +++ b/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/README.md @@ -175,23 +175,22 @@ class Solution { class Solution { public: TreeNode* lcaDeepestLeaves(TreeNode* root) { + auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair { + 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 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}; - } }; ``` @@ -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; + } +} +``` + diff --git a/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/README_EN.md b/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/README_EN.md index 91f045fc4c941..55ff2ddf2357e 100644 --- a/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/README_EN.md +++ b/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/README_EN.md @@ -172,23 +172,22 @@ class Solution { class Solution { public: TreeNode* lcaDeepestLeaves(TreeNode* root) { + auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair { + 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 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}; - } }; ``` @@ -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; + } +} +``` + diff --git a/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/Solution.cpp b/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/Solution.cpp index 9bd4cf609cab8..67e9fb4f3f1fb 100644 --- a/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/Solution.cpp +++ b/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/Solution.cpp @@ -12,21 +12,20 @@ class Solution { public: TreeNode* lcaDeepestLeaves(TreeNode* root) { + auto dfs = [&](this auto&& dfs, TreeNode* root) -> pair { + 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 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}; - } }; \ No newline at end of file diff --git a/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/Solution.cs b/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/Solution.cs new file mode 100644 index 0000000000000..1667eef3fbeca --- /dev/null +++ b/solution/1100-1199/1123.Lowest Common Ancestor of Deepest Leaves/Solution.cs @@ -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; + } +}