leetcode 之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].

即 给定一颗二叉树。 使用后序遍历输出。 不用递归的方式。

首先递归的方式:后续遍历的话先访问左子树,然后访问右子树,然后访问根节点。

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def postorderTraversal(self, root):
10         """
11         :type root: TreeNode
12         :rtype: List[int]
13         """
14         res = []
15         if not root:
16             return res
17         self.get(root, res)
18         return res
19
20     def get(self, root, res):
21         if root.left:
22             self.get(root.left, res)
23         if root.right:
24             self.get(root.right, res)
25         res.append(root.val)
26                  

非递归的思想是,需要向左找到叶子节点,并将节点依次放入栈中,并标记这些节点的左子树已经被访问了一次了。然后判断叶子节点的右子树是否存在,如果叶子节点右子树存在,继续向左找到叶子节点。。。并标记该节点的右子树已经访问过了。

代码如下:

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def postorderTraversal(self, root):
10         """
11         :type root: TreeNode
12         :rtype: List[int]
13         """
14         res = []
15         tmp = []
16         flag = []  // 右子树被访问标记
17         flag1 = [] // 左子树被访问标记
18         if not root:
19             return res
20         tmp.append(root)
21         while tmp:
22             while root.left and root not in flag1:
23                 tmp.append(root.left)
24                 flag1.append(root)
25                 root = root.left
26             node = tmp[-1]
27             if  node not in flag:
28                 if node.right:
29                     tmp.append(node.right)
30                     flag.append(node)
31                     root = node.right
32                     continue
33                 else:
34                     root = tmp.pop()
35                     res.append(root.val)
36             else:
37                 root = tmp.pop()
38                 res.append(root.val)
39         return res
40                  
时间: 2024-10-06 09:31:45

leetcode 之Binary Tree Postorder Traversal的相关文章

【Leetcode】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? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

leetcode题解: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? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 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: Recur

LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 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: Recur

[LeetCode][JavaScript]Binary Tree Postorder Traversal

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? 同样的配方,同样的味道.

【LeetCode】Binary Tree Postorder Traversal (3 solutions)

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? 解法一:递归法 /** *

Leetcode dfs Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Total Accepted: 28560 Total Submissions: 92333My Submissions 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 s

Java for LeetCode 145 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]. 后序遍历,左子树→右子树→根节点 前序遍历的非递归实现需要一个计数器,方法是需要重写一个类继承TreeNode,翁慧玉教材<数据结构:题解与拓展>P113有详细介绍,这里略.递归JAVA实现如下: public Lis

leetcode - [6]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]. 思路:后序遍历是按照“左子树,右子树,根”的顺序访问元素.那么根或者其它父亲元素就要先压入栈,然后再弹出. #include <iostream> #include <algorithm> #include

leetcode 1145. Binary Tree Postorder Traversal ----- java

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? 求后序遍历,要求不使用递归. 使用栈,从后向前添加. /** * Definition f