Registry Mirror
搜索
Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Files
Expand file tree
master
Breadcrumbs
leetcode
/
algorithms
/
python
/
SecondMinimumNodeInABinaryTree
/
findSecondMinimumValue.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
15 lines (14 loc) · 705 Bytes
master
Breadcrumbs
leetcode
/
algorithms
/
python
/
SecondMinimumNodeInABinaryTree
/
findSecondMinimumValue.py
Copy path
Top
File metadata and controls
Code
Blame
15 lines (14 loc) · 705 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"""
because of the special porperty of the tree, the question basically is asking to find
the smallest element in the subtree of root. So, we can recursively find left and right
value that is not equal to the root's value, and then return the smaller one of them
"""
def findSecondMinimumValue(self, root):
if not root: return -1
if not root.left and not root.right: return -1
left, right = root.left.val, root.right.val
if left == root.val: left = self.findSecondMinimumValue(root.left)
if right == root.val: right = self.findSecondMinimumValue(root.right)
if left != -1 and right != -1: return min(left, right)
if left == -1: return right
if right == -1: return left
You can’t perform that action at this time.