python简单二叉树遍历代码,python二叉树,<python简单二叉树
python简单二叉树遍历代码,python二叉树,<python简单二叉树遍历代码>标签: <无>1.[python简单二叉树遍历代码代码][Python]代码
#B treeclass TreeNode: def __init__(self,x): self.val=x self.left=None self.right=Nonedef builtTree(): root=None val=input("Enter the value:") if(val=='#'): pass else: root=TreeNode(val) root.left=builtTree() root.right=builtTree() return rootdef PreTraver(root): if root==None: return else: print(root.val,end=" ") traver(root.left) traver(root.right)def MidTraver(root): if root==None: return MidTraver(root.left) print(root.val,end=" ") MidTraver(root.right)def ReTraver(root): if root==None: return ReTraver(root.left) ReTraver(root.right) print(root.val,end=" ")def deepth(root): if root==None: return 1 leftDeepth=deepth(root.left)+1 rightDeepth=deepth(root.right)+1 if leftDeepth>rightDeepth: return leftDeepth else: return rightDeepthdef main(): root=builtTree() if(root==None): print("builtTree failed")if __name__=='__main__': main()else: print("test.py has worked")
编橙之家文章,
1.[python简单二叉树遍历代码代码][Python]代码
#B treeclass TreeNode: def __init__(self,x): self.val=x self.left=None self.right=Nonedef builtTree(): root=None val=input("Enter the value:") if(val=='#'): pass else: root=TreeNode(val) root.left=builtTree() root.right=builtTree() return rootdef PreTraver(root): if root==None: return else: print(root.val,end=" ") traver(root.left) traver(root.right)def MidTraver(root): if root==None: return MidTraver(root.left) print(root.val,end=" ") MidTraver(root.right)def ReTraver(root): if root==None: return ReTraver(root.left) ReTraver(root.right) print(root.val,end=" ")def deepth(root): if root==None: return 1 leftDeepth=deepth(root.left)+1 rightDeepth=deepth(root.right)+1 if leftDeepth>rightDeepth: return leftDeepth else: return rightDeepthdef main(): root=builtTree() if(root==None): print("builtTree failed")if __name__=='__main__': main()else: print("test.py has worked")
编橙之家文章,
相关内容
- python新手关于继承的练习代码,python新手继承,<pyth
- linux下使用python根据IP地址子网掩码计算网段,linuxpyt
- 用Python在指定目录搜索需要的文件名,python文件名,用
- python源码实现猜位置游戏,python源码猜位置,python源码实
- python代码来关闭显示器,python代码显示器,python代码来关
- Mac osx系统下python自动登录服务器,osxpython,Mac osx系统下
- 身份证号码解析python代码,解析python,身份证号码解析
- python模拟用户登录v2ex网站,python用户登录v2ex,python模拟
- 每隔固定时间抓取百度 python代码,抓取python,<每隔固
- wxpython 源码 情人节颤抖,wxpython颤抖,wxpython 源码
评论关闭