diff options
| author | 冯昶 <seaker@qq.com> | 2022-02-10 17:28:53 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2022-02-10 17:28:53 +0800 |
| commit | 24d07638f45f70838ba3721106c5c4f6d992f69e (patch) | |
| tree | 618c14fbb4615765f95353129863d3ad85a88867 /challenge-151/eric-cheung/python | |
| parent | 1ff224c623b04c549b24ec402ea43131cd910588 (diff) | |
| parent | af7e6f300ff3d68b38412f2be2a4fc845ae19a79 (diff) | |
| download | perlweeklychallenge-club-24d07638f45f70838ba3721106c5c4f6d992f69e.tar.gz perlweeklychallenge-club-24d07638f45f70838ba3721106c5c4f6d992f69e.tar.bz2 perlweeklychallenge-club-24d07638f45f70838ba3721106c5c4f6d992f69e.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-151/eric-cheung/python')
| -rwxr-xr-x | challenge-151/eric-cheung/python/ch-1.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-151/eric-cheung/python/ch-1.py b/challenge-151/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..cab9c88121 --- /dev/null +++ b/challenge-151/eric-cheung/python/ch-1.py @@ -0,0 +1,43 @@ +## Credit:
+## https://www.educative.io/edpresso/finding-the-maximum-depth-of-a-binary-tree
+
+class Node:
+ def __init__(self , val):
+ self.value = val
+ self.left = None
+ self.right = None
+
+def maxDepth(root):
+ ## Null node has 0 depth.
+ if root == None:
+ return 0
+
+ ## Get the depth of the left and right subtree
+ ## using recursion.
+ leftDepth = maxDepth(root.left)
+ rightDepth = maxDepth(root.right)
+
+ ## Choose the larger one and add the root to it.
+ if leftDepth > rightDepth:
+ return leftDepth + 1
+ else:
+ return rightDepth + 1
+
+## Driver Code
+
+## Example 1:
+## root = Node(1)
+## root.left = Node(2)
+## root.right = Node(3)
+## root.left.left = Node(4)
+## root.left.right = Node(5)
+
+## Example 2:
+root = Node(1)
+root.left = Node(2)
+root.right = Node(3)
+root.left.left = Node(4)
+root.right.right = Node(5)
+root.left.left.right = Node(6)
+
+print("The max depth is:", maxDepth(root))
\ No newline at end of file |
