Created
January 26, 2014 23:04
-
-
Save yekmer/8640656 to your computer and use it in GitHub Desktop.
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 DeepestDepth { | |
static DeepestDepth ts = new DeepestDepth(); | |
static Node maxNode; | |
static int maxDepth = 0; | |
public static void main(String[] args) { | |
Node l1221 = ts.new Node(null, null, 8); | |
Node l122 = ts.new Node(null, l1221, 4); | |
Node l121 = ts.new Node(null, null, 7); | |
Node r12 = ts.new Node(null, null, 4); | |
Node l12 = ts.new Node(l122, l121, 5); | |
Node l11 = ts.new Node(null, null, 2); | |
Node r1 = ts.new Node(r12, null, 5); | |
Node l1 = ts.new Node(l12, l11, 3); | |
Node root = ts.new Node(r1, l1, 6); | |
deepestDepth(root); | |
System.out.println(maxNode.value); | |
} | |
public static void deepestDepth(Node root) { | |
dfs(root, 1); | |
} | |
public static void dfs(Node node, int depth) { | |
if (node == null) | |
return; | |
if (depth > maxDepth) { | |
maxDepth = depth; | |
maxNode = node; | |
} | |
depth++; | |
dfs(node.right, depth); | |
dfs(node.left, depth); | |
} | |
class Node { | |
Node right; | |
Node left; | |
int value; | |
public Node(Node right, Node left, int value) { | |
this.right = right; | |
this.left = left; | |
this.value = value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment