LeetCode: 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?

Show Tags
Have you met this question in a real interview? Yes  No
Discuss

SOLUTION 1:

递归解法

 1 public List<Integer> postorderTraversal1(TreeNode root) {
 2         List<Integer> ret = new ArrayList<Integer>();
 3         dfs(root, ret);
 4         return ret;
 5     }
 6
 7     // Solution 1: rec
 8     public void dfs(TreeNode root, List<Integer> ret) {
 9         if (root == null) {
10             return;
11         }
12
13         dfs(root.left, ret);
14         dfs(root.right, ret);
15         ret.add(root.val);
16     }

SOLUTION 2:

/**
     *  后序遍历迭代解法
     *  http://www.youtube.com/watch?v=hv-mJUs5mvU
     *  http://blog.csdn.net/tang_jin2015/article/details/8545457
     *  从左到右的后序 与从右到左的前序的逆序是一样的,所以就简单喽! 哈哈
     *  用另外一个栈进行翻转即可喽
     */

 1 // Solution 2: iterator
 2     public List<Integer> postorderTraversal(TreeNode root) {
 3         List<Integer> ret = new ArrayList<Integer>();
 4         if (root == null) {
 5             return ret;
 6         }
 7
 8         Stack<TreeNode> s = new Stack<TreeNode>();
 9         Stack<Integer> out = new Stack<Integer>();
10
11         s.push(root);
12
13         while (!s.isEmpty()) {
14             TreeNode cur = s.pop();
15             out.push(cur.val);
16
17             if (cur.left != null) {
18                 s.push(cur.left);
19             }
20
21             if (cur.right != null) {
22                 s.push(cur.right);
23             }
24         }
25
26         while (!out.isEmpty()) {
27             ret.add(out.pop());
28         }
29
30         return ret;
31     }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/PostorderTraversal.java

时间: 2024-10-06 18:13:42

LeetCode: Binary Tree Postorder Traversal 解题报告的相关文章

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51494797 Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级别 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree

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 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 Postorder Traversal [145]

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

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? 中文:二叉树的后续遍历(左-右-根).能用非递归吗? 递归: public class

LeetCode 144. Binary Tree Preorder Traversal 解题报告

144. Binary Tree Preorder Traversal My Submissions Question Total Accepted: 108336 Total Submissions: 278322 Difficulty: Medium Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3

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

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? Show Tags #include<iostream> #include<vec

[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? 经典题目,求二叉树的后序遍历的非递归方法,跟前序,中序,层序一样都需要用到栈,后续的顺序