Last active
January 6, 2016 17:40
-
-
Save rdtr/5a079f9ffb4163763ca1 to your computer and use it in GitHub Desktop.
algorithms_trees_find_maximum_element_in_binary_tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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