[LeetCode]题解(python):144-Binary Tree Preorder Traversal
题目来源:
https://leetcode.com/problems/binary-tree-preorder-traversal/
题意分析:
前序遍历一棵树,递归的方法很简单。那么非递归的方法呢。
题目思路:
前序遍历的顺序是先遍历根节点,再遍历左子树,最后遍历右子树。递归的方法很直观。非递归的方法是利用栈来实现,后进先出,先放右子树进入栈。代码给的是非递归的方法。
代码(python):
# 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 preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans = []
if root == None:
return ans
stack = [root]
while len(stack) != 0:
p = stack.pop()
ans.append(p.val)
if p.right:
stack.append(p.right)
if p.left:
stack.append(p.left)
return ans
[LeetCode]题解(python):144-Binary Tree Preorder Traversal的更多相关文章
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- Java for LeetCode 144 Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Java [Leetcode 144]Binary Tree Preorder Traversal
题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...
- 【LeetCode】144. Binary Tree Preorder Traversal
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
随机推荐
- CentOS 6使用VNC配置远程桌面
首先,配置vncservers(注意,rootW为1,普通用户按2,3以此类推) [root@hadoop1001 hadoop]# vi /etc/sysconfig/vncservers # VN ...
- C#中一个问号和两个问号(a ?? b)的作用
不卖关子,直接开门见山: C#中两个问号的作用是判断??左边的对象是否为null,如果不为null则使用??左边的对象,如果为null则使用??右边的对象. 比如:a = b ?? c,如果b为nul ...
- C++自定义命名空间
关于C++自定义命名空间,今天验证了一下命名空间如何使用,和嵌套命名空间以及出现的bug. 如何自定义命名空间,实例如下: insertion_sort.h和insertion_sort.cpp #p ...
- lightoj 1030
递推,倒着递推. #include<stdio.h> #define maxn 1010 #define min(a,b) (a)>(b)?(b):(a) int main() { ...
- 在MFC主对话框OnInitDialog()中弹出对话框
BOOL CXXXDlg::OnInitDialog(){ CDialogEx::OnInitDialog(); SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FA ...
- 不要伤害指针(1)--运算符&和*
原文转载地址:http://blog.csdn.net/sunchaoenter/article/details/6646001 增加自己的想法,作为笔记. 这里&是取地址运算符,*是间接运算 ...
- Linux命令之ifconfig
许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...
- 诞生于饭桌上的jcSQL语言
相信每个Coder都有心在自己求学阶段可以写一门自己的语言,无论是毕业设计,还是课余爱好:不管是为了提升B格,还是想练手,抑或对其他语言不满,想自己撸一个,只要坚持下去了,都是不错的理由. 现在正值暑 ...
- MVC自学第一课
了解传统的ASP.NET WebForm ASP.NET 在02年问世,给Web开发领域带来了巨大转变.下图描述了当时微软的技术堆栈. ASP.NET WebForm 技术堆栈 (注:此图的含义为,W ...
- Linux 分区初始化为物理卷,把物理卷加入卷组
用到的命令有 1.pvcreate (physical volume create) 2.vgcreate (volume group create) 例子1:创建物理卷 pvcreate /dev/ ...