博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Binary Tree的3种非Recursive遍历
阅读量:4594 次
发布时间:2019-06-09

本文共 3586 字,大约阅读时间需要 11 分钟。

Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values.

For example:

Given binary tree {1,#,2,3},

1    \     2    /   3

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

'''Created on Nov 18, 2014@author: ScottGu
'''# Definition for a binary tree node# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # @param root, a tree node # @return a list of integers def preorderTraversal(self, root): stack=[] vals=[] if(root==None): return vals node=root stack.append(node) while(len(stack)!=0): node=stack.pop() if(node==None): continue vals.append(node.val) stack.append(node.right) stack.append(node.left) return vals
View Code

 

Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values.

For example:

Given binary tree {1,#,2,3},

1    \     2    /   3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

confused what "{1,#,2,3}" means? 

'''Created on Nov 18, 2014@author: ScottGu
'''# Definition for a binary tree node# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # @param root, a tree node # @return a list of integers def inorderTraversal(self, root): stack=[] vals=[] visited={} if(root==None): return vals node=root stack.append(node) visited[node]=1 while(len(stack)!=0): if(node.left!=None and visited.has_key(node.left)==False): node=node.left stack.append(node) visited[node]=1 else: node=stack.pop() if(node==None): continue vals.append(node.val) if(node.right!=None): stack.append(node.right) node=node.right return vals
View Code

 

Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values.

For example:

Given binary tree {1,#,2,3},

1    \     2    /   3

return [3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

'''Created on Nov 19, 2014@author: ScottGu
'''# Definition for a binary tree node# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # @param root, a tree node # @return a list of integers def postorderTraversal(self, root): visited={} stack=[] vals=[] if(root==None): return vals node=root stack.append(node) visited[node]=1 while(len(stack)!=0): node=stack[-1] if(node.left !=None and visited.has_key(node.left)==False): stack.append(node.left) visited[node.left]=1 continue else: if(node.right!=None and visited.has_key(node.right)==False): stack.append(node.right) visited[node.right]=1 continue node=stack.pop() if(node==None): continue vals.append(node.val) return vals
View Code

 

 

 

转载于:https://www.cnblogs.com/scottgu/p/4108227.html

你可能感兴趣的文章
(Python第四天)字符串
查看>>
个人介绍
查看>>
使用python动态特性时,让pycharm自动补全
查看>>
关于R软件的安装
查看>>
MySQL数据库免安装版配置
查看>>
你必知必会的SQL面试题
查看>>
html5 Canvas绘制时钟以及绘制运动的圆
查看>>
Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween
查看>>
JavaScript空判断
查看>>
洛谷 P1439 【模板】最长公共子序列(DP,LIS?)
查看>>
python timeit
查看>>
Wireless Network 并查集
查看>>
51nod 1019 逆序数
查看>>
Java_Set用法总结
查看>>
Codeforces Round #160 (Div. 2) D. Maxim and Restaurant(DP)
查看>>
Exchange Port
查看>>
oracle 01578
查看>>
在source中查看代码
查看>>
pip install xxx -i https://pypi.tuna.tsinghua.edu.cn/simple
查看>>
apache环境下配置多个ssl证书搭建多个站点
查看>>