/**
* Problem: LeetCode 865 - Smallest Subtree with all the Deepest Nodes
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private static class Result {
int depth;
TreeNode node;
Result(int depth, TreeNode node) {
this.depth = depth;
this.node = node;
}
}
public TreeNode subtreeWithAllDeepest(TreeNode root) {
return dfs(root).node;
}
private Result dfs(TreeNode node) {
if (node == null) {
return new Result(0, null);
}
Result left = dfs(node.left);
Result right = dfs(node.right);
if (left.depth == right.depth) {
return new Result(left.depth + 1, node);
} else if (left.depth > right.depth) {
return new Result(left.depth + 1, left.node);
} else {
return new Result(right.depth + 1, right.node);
}
}
}
"""
Problem: LeetCode 865 - Smallest Subtree with all the Deepest Nodes
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import Optional, Tuple
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def subtreeWithAllDeepest(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
def dfs(node: Optional[TreeNode]) -> Tuple[int, Optional[TreeNode]]:
if not node:
return 0, None
left_depth, left_node = dfs(node.left)
right_depth, right_node = dfs(node.right)
if left_depth == right_depth:
return left_depth + 1, node
elif left_depth > right_depth:
return left_depth + 1, left_node
else:
return right_depth + 1, right_node
return dfs(root)[1]
/**
* Problem: LeetCode 865 - Smallest Subtree with all the Deepest Nodes
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <utility>
#include <algorithm>
using namespace std;
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
pair<int, TreeNode*> dfs(TreeNode* node) {
if (!node) {
return {0, nullptr};
}
auto left = dfs(node->left);
auto right = dfs(node->right);
if (left.first == right.first) {
return {left.first + 1, node};
} else if (left.first > right.first) {
return {left.first + 1, left.second};
} else {
return {right.first + 1, right.second};
}
}
public:
TreeNode* subtreeWithAllDeepest(TreeNode* root) {
return dfs(root).second;
}
};
/**
* Problem: LeetCode 865 - Smallest Subtree with all the Deepest Nodes
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var subtreeWithAllDeepest = function(root) {
function dfs(node) {
if (!node) {
return { depth: 0, node: null };
}
const left = dfs(node.left);
const right = dfs(node.right);
if (left.depth === right.depth) {
return { depth: left.depth + 1, node: node };
} else if (left.depth > right.depth) {
return { depth: left.depth + 1, node: left.node };
} else {
return { depth: right.depth + 1, node: right.node };
}
}
return dfs(root).node;
};