Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdtr/5a079f9ffb4163763ca1 to your computer and use it in GitHub Desktop.
Save rdtr/5a079f9ffb4163763ca1 to your computer and use it in GitHub Desktop.
algorithms_trees_find_maximum_element_in_binary_tree
/* The definition of a node of tree is like:
public class Node {
public int value;
public Node left;
public Node right;
public Node(int value) {
this.value = value;
left = null;
right = null;
}
public setValue(int value) {
this.value = value;
}
public setLeft(Node left) {
this.left = left;
}
public setRight(Node right) {
this.right = right;
}
}
*/
public class Solution {
public static int findMax(Node root){
if (root == null) return Integer.MIN_VALUE;
int maxValue = root.value;
List<Node> lst = new ArrayList<Node>(root);
while (!lst.isEmpty()) {
List<Node> newLst = new ArrayList<Node>();
if (lst.left != null) {
newLst.add(lst.left);
if (lst.left > maxValue) maxValue = lst.left;
if (lst.right != null) {
newLst.add(lst.right);
if (lst.right > maxValue) maxValue = lst.right;
}
lst = newLst;
}
return maxValue;
}
}
'''
The definition of tree node is like:
class Node():
def __init__(self, value):
self.value, self.left, self.right = value, None, None
def setValue(self, value):
self.value = value
def setLeft(self, left):
self.left = left
def setRight(self, right):
self.right = right
'''
def find_max(root):
if not root:
return None
queue = [root]
max_value = root.value
while queue:
new_queue = []
for node in queue:
if node.left:
max_value = max(max_value, node.left.value)
new_queue.append(node.left)
if node.right:
max_value = max(max_value, node.right.value)
new_queue.append(node.right)
queue = new_queue
return max_value
public class Solution {
public static int findMaxFromParentAndChildren(Node node) {
if (node == null) return Integer.MIN_VALUE;
if (node.left != null && node.right != null) {
return Math.max(node.value,
Math.max(Solution.findMaxFromParentAndChildren(left),
Solution.findMaxFromParentAndChildren(right)));
} else if (node.left != null) {
return Math.max(node.value,
Solution.findMaxFromParentAndChildren(left));
} else if (node.right != null) {
return Math.max(node.value,
Solution.findMaxFromParentAndChildren(right));
} else {
return node.value;
}
}
public static int findMax(Node root){
return Solution.findMaxFromParentAndChildren(root);
}
}
def find_max_value_from_parent_and_children(node):
if not node:
return None
if node.left and node.right:
return max(node.value, max(find_max_value_from_parent_and_children(node.left), \
find_max_value_from_parent_and_children(node.right)))
elif node.left:
return max(node.value, find_max_value_from_parent_and_children(node.left))
elif node.right:
return max(node.value, find_max_value_from_parent_and_children(node.right))
else:
node.value
def find_max(root):
return find_max_value_from_parent_and_children(root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment