[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的右子树为空,继续出栈,2出栈后,2的右子树非空,5入栈,5的左子树为空,5出栈,由于5的右子树为空,继续出栈,1出栈后,发现1的右子树非空,3入栈。。。依次类推,就完成了非递归(迭代)解法。

代码:


# 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_inorder(self, root, list):
stack = []
while root or stack:
if root:
stack.append(root)
root = root.left
else:
root = stack.pop()
list.append(root.val)
root = root.right
return list

def recursive_inorder(self, root, list):
if root:
self.inorder(root.left, list)
list.append(root.val)
self.inorder(root.right, list)

def inorderTraversal(self, root):
list = []
self.iterative_inorder(root, list)
return list

[leetcode]Binary Tree Inorder Traversal @ Python

时间: 2024-10-06 01:09:52

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

LeetCode: Binary Tree Inorder Traversal [094]

[题目] 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? >

[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}.

LeetCode——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? 原题链接:https://oj.leetcode.com/problems/binary-t

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node

Leetcode: 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? 递归解法(C++): /** * Definition for binary tre

LeetCode: Binary Tree Inorder Traversal 解题报告

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

[LeetCode] 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? > read

[LeetCode] 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? > read

leetcode Binary Tree Postorder 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 postorderTraversal(self, root): """ :type root: TreeNode :rtype: List[