Skip to content

Commit fb72abf

Browse files
authored
Merge pull request HuberTRoy#5 from hanhaotian/master
update Solution
2 parents 0bc0920 + 6502189 commit fb72abf

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

BFS/MaximumDepthOfBinaryTree.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,13 @@
2828
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
2929
3030
"""
31+
class Solution:
32+
33+
def maxDepth(self, root: TreeNode) -> int:
34+
if root is None:
35+
return 0
36+
else:
37+
left_height = self.maxDepth(root.left)
38+
right_height = self.maxDepth(root.right)
39+
return max(left_height, right_height) + 1
40+

Tree/KthSmallestElementInABST.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,18 @@
5656
# self.left = None
5757
# self.right = None
5858

59-
class Solution(object):
60-
def kthSmallest(self, root, k):
61-
"""
62-
:type root: TreeNode
63-
:type k: int
64-
:rtype: int
65-
"""
66-
self.inorder_result = []
67-
68-
def inorder(root):
69-
if root.left:
70-
inorder(root.left)
71-
72-
self.inorder_result.append(root.val)
73-
74-
if root.right:
75-
inorder(root.right)
76-
77-
inorder(root)
78-
79-
return self.inorder_result[k-1]
59+
class Solution:
60+
61+
def find_data(self, root: TreeNode):
62+
63+
if root is None:
64+
return
65+
Solution.find_data(self,root.left)
66+
self.data.append(root.val)
67+
Solution.find_data(self,root.right)
68+
return
69+
70+
def kthSmallest(self, root: TreeNode, k: int) -> int:
71+
self.data = []
72+
Solution.find_data(self, root)
73+
return self.data[k-1]

0 commit comments

Comments
 (0)