码迷,mamicode.com
首页 > 其他好文 > 详细

700. Search in a Binary Search Tree

时间:2018-12-17 02:12:55      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:for   int   代码   info   else   com   nbsp   数据结构   not   

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def searchBST(self, root, val):
        """
        :type root: TreeNode
        :type val: int
        :rtype: TreeNode
        """
        if root==None : return NULL
        
        if root.val==val: return root
        
        elif root.val<val:
            self.searchBST(root.right,val)
        else:
            self.searchBST(root.left,val)

问题:不明白树的数据结构在python中是如何实现的?

 

    def __init__(self, root_value):
        self.root = root_value
        self.leftchild = None
        self.rightchild = None

 

 

 

终于看,明白了,如图:

技术分享图片

 

 

上面的代码有问题:

class Solution(object):
    def searchBST(self, root, val):
        """
        :type root: TreeNode
        :type val: int
        :rtype: TreeNode
        """
        if not root:
            return None
        if root.val == val:
            return root
        elif root.val < val:
            return self.searchBST(root.right, val)
        else:
            return self.searchBST(root.left, val)
        

 

700. Search in a Binary Search Tree

标签:for   int   代码   info   else   com   nbsp   数据结构   not   

原文地址:https://www.cnblogs.com/captain-dl/p/10128945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!