[leetcode]Binary Tree Preorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/

题意:这题用递归比较简单。应该考察的是使用非递归实现二叉树的先序遍历。先序遍历的遍历顺序是:根,左子树,右子树。

解题思路:如果树为下图:

                      1

                     /     \

                    2         3

                   /     \    /    \

                   4       5  6     7

    使用一个栈。步骤为:

    一,先遍历节点1,并入栈,如果有左孩子,继续遍历并入栈,一直到栈为{1,2,4}。

    二,开始弹栈,当栈顶元素为2时,弹出2,并检测2存在右孩子5,于是遍历5并入栈,此时栈为{1,5}。

    三,弹出5,5没有左右孩子,继续弹栈,将1弹出后,栈为{}。

    四,由于1存在右孩子,则继续按照以上步骤入栈出栈。{3, 6}->{7}->{},结束。

    栈的状态变化:{1}->{1,2}->{1,2,4}->{1,2}->{1}->{1,5}->{1}->{}->{3}->{3,6}->{3}->{}->{7}->{}。

代码:


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

class Solution:
# @param root, a tree node
# @return a list of integers
def iterative_preorder(self, root, list):
stack = []
while root or stack:
if root:
list.append(root.val)
stack.append(root)
root = root.left
else:
root = stack.pop()
root = root.right
return list

def recursive_preorder(self, root, list):
if root:
list.append(root.val)
self.preorder(root.left,list)
self.preorder(root.right,list)

def preorderTraversal(self,root):
list = []
self.iterative_preorder(root,list)
return list

[leetcode]Binary Tree Preorder Traversal @ Python

时间: 2024-10-07 05:31:11

[leetcode]Binary Tree Preorder Traversal @ Python的相关文章

leetcode - Binary Tree Preorder Traversal && Binary Tree Inorder Traversal && Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

LeetCode: Binary Tree Preorder Traversal [144]

[题目] 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? [题意] 非递归返回先序遍历结果 [思路] 维护一个栈即可 [代码] /** *

[leetcode]Binary Tree Inorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 题意:二叉树的中序遍历. 解题思路:这道题用递归解不难,所以应该考察的是非递归求解二叉树的中序遍历.我们使用一个栈来解决问题.比如一颗二叉树为{1,2,3,4,5,6,7},第一层为{1},第二层为{2,3},第三层为{4,5,6,7}.那么我们依次存储左子树的根节点,那么入栈顺序为:1,2,4.由于4的左子树为空,所以开始出栈.4出栈,检查4的右子树为空,继续

LeetCode: Binary Tree Preorder Traversal 解题报告

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    /   3return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively?

[LeetCode] Binary Tree Preorder Traversal (非递归的先序遍历)

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 解题思路: 二叉树的前序遍历.

【LeetCode]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? 思路: 写个非递归的前序遍历,用 stack. 代码: C++: /** * De

LeetCode——Binary Tree Preorder Traversal

Description: 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]. 经典的二叉树前序遍历,用递归闭着眼写. /** * Definition for a binary tree node. * public class TreeNode { * int val; *

Leetcode: 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? 二叉树的前序遍历 先看递归的写法(C++): /** * Definition f

[LeetCode] 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? http://www.cnblogs.com/dolphin0520/archive/201