【LeetCode with Python】 Binary Tree Preorder Traversal
来源:互联网 发布:three.min.js下载 编辑:程序博客网 时间:2024/05/21 17:21
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/binary-tree-preorder-traversal/
题目类型:
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/3465737
原题页面:https://oj.leetcode.com/problems/binary-tree-preorder-traversal/
题目类型:
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/3465737
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?
非递归前序遍历二叉树。这里是用栈,先右孩子入栈,再左孩子入栈。隐约还记得如果是层次遍历,即需要用队列了。二叉树的非递归遍历中,记得后序遍历是最复杂的,需要用两个栈,以后再复习一下后序遍历。
class Solution: # @param root, a tree node # @return a list of integers def preorderTraversal(self, root): if None == root: return [ ] list = [ ] stack = [ ] cur = root while True: list.append(cur.val) if None != cur.right: stack.append(cur.right) if None != cur.left: stack.append(cur.left) if len(stack) >= 1: cur = stack.pop() else: break return list
- 【LeetCode with Python】 Binary Tree Preorder Traversal
- Leetcode -- Python -- Binary Tree Preorder Traversal
- leetcode [python] 【144】 Binary Tree Preorder Traversal
- 【Leetcode】【python】Binary Tree Preorder Traversal
- LeetCode Binary Tree Preorder Traversal
- 【LeetCode】Binary Tree Preorder Traversal
- Leetcode: Binary Tree Preorder Traversal
- Leetcode Binary Tree Preorder Traversal
- LeetCode:Binary Tree Preorder Traversal
- Leetcode: Binary Tree Preorder Traversal
- LeetCode:Binary Tree Preorder Traversal
- [LeetCode]Binary Tree Preorder Traversal
- [LeetCode] Binary Tree Preorder Traversal
- LeetCode | Binary Tree Preorder Traversal
- Leetcode: Binary Tree Preorder Traversal
- [LeetCode] Binary Tree Preorder Traversal
- LeetCode - Binary Tree Preorder Traversal
- LeetCode - Binary Tree Preorder Traversal
- 【LeetCode with Python】 Maximum Subarray
- 40种网站设计常用技巧
- 【LeetCode with Python】 Linked List Cycle
- 疯狂的世界,疯狂的人!
- 【LeetCode with Python】 Insertion Sort List
- 【LeetCode with Python】 Binary Tree Preorder Traversal
- JDOM入门:使用JDOM解析XML
- 开始→运行→输入的命令集锦( 菜鸟必读)
- 【LeetCode with Python】 Binary Tree Inorder Traversal
- 【LeetCode with Python】 Best Time to Buy and Sell Stock II
- 开发自己的窗体设计器
- 用Javascript显示隐藏层
- 用SAX解析XML的例子
- SD2C大会视频