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<vector>
#include<stack>
#include<list>
using namespace std;

//二叉树的定义
struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

//采用递归的方法来做:
void post_search(TreeNode* root,vector<int>& vec)
{
	if(root==NULL)
		return;
	post_search(root->left,vec);
	post_search(root->right,vec);
	vec.push_back(root->val);
	return;
}
vector<int> postorderTraversal(TreeNode *root) {
	vector<int> last_result;
	post_search(root,last_result);
	return last_result;
}

/*
这道题目是“二叉树的后序遍历”。通常有两种做法,递归求解以及循环求解。

递归求解,较为简单,先访问左孩子结点、在访问右孩子节点,访问当前节点。
详细可参照如下代码。

循环求解,较为麻烦。但是相对于递归求解而言,这种方法耗费的函数栈空间更小,
并且省去了大量的函数调用时间的开销,速度更加快,

*/
//采用堆栈的方法来做
vector<int> postorderTraversal(TreeNode *root)
{
	stack<TreeNode*> temp;
	vector<int> result_last;
	TreeNode* temp_node;

	temp.push(root);
	while

}
int main()
{
	system("psuse");
	return 1;
}

  

时间: 2025-01-17 16:35:22

leetcode——Binary Tree Postorder Traversal(递归,栈)的相关文章

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

二叉树的各种遍历算法-leetcode Binary Tree Postorder Traversal 扩展

二叉树的各种遍历方法有  前序遍历   中序遍历    后序遍历  层序遍历.其中前三种遍历有递归程序可以实现,但是我们也有必要掌握其非递归版本的算法实现.正好在leetcode中遇到了遍历二叉树的问题,今天在这里一并总结了. 首先,引用leetcode中关于二叉树节点的定义. 1 // Definition for binary tree 2 struct TreeNode { 3 int val; 4 TreeNode *left; 5 TreeNode *right; 6 TreeNode

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

[LeetCode] Binary Tree Postorder Traversal dfs,深度搜索

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? Hide Tags Tree Stack 一题后续遍历树的问题,很基础,统计哪里的4ms

LeetCode Binary Tree Postorder Traversal(数据结构)

题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }

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[