144.Binary Tree Preorder Traversal(非递归前序遍历)

Given a binary tree, return the preorder traversal of itsnodes‘ values.

For example:

Given binary tree {1,#,2,3},

1

\

2

/

3

return [1,2,3].

Note: Recursive solution istrivial, could you do it iteratively?

HideTags

Tree Stack

#pragma once
#include<iostream>
#include<vector>
#include<stack>
using namespace std;

struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

vector<int> preorderTraversal(TreeNode *root) {
	vector<int> result;
	stack<TreeNode*> s;
	TreeNode* p = root;
	while (p)
	{
		result.push_back(p->val);
		if (p->left)//有左,向左,右压栈
		{
			if (p->right)
				s.push(p->right);
			p = p->left;
		}
		else if (p->right)//无左,有右,向右
			p = p->right;
		else if (!s.empty())//无左无右,栈不空,pop
		{
			p = s.top();
			s.pop();
		}
		else
			return result;//无左无右栈空,返回
	}
	return result;//不可少,若root==NULL,用这一句返回
}

void main()
{
	TreeNode* t1 = new TreeNode(1);
	TreeNode* t2 = new TreeNode(2);
	TreeNode* t3 = new TreeNode(3);
	TreeNode* t4 = new TreeNode(4);
	TreeNode* t5 = new TreeNode(5);
	TreeNode* t6 = new TreeNode(6);
	TreeNode* t7 = new TreeNode(7);
	TreeNode* t8 = new TreeNode(8);
	TreeNode* t9 = new TreeNode(9);

	TreeNode* t0 = new TreeNode(0);
	TreeNode* t10 = NULL;

	t1->right = t2;
	t2->left = t3;

	t4->left = t5;
	t4->right = t6;

	t7->right = t8;
	t8->right = t9;

	t1->left = t7;
	t7->left = t4;

	vector<int>result = preorderTraversal(t1);
	for (int i = 0; i < (int)result.size(); i++)
		cout << result[i] << " ";
	cout << endl;
	system("pause");

}
时间: 2024-12-19 04:49:27

144.Binary Tree Preorder Traversal(非递归前序遍历)的相关文章

leetcode 144. Binary Tree Preorder Traversal 二叉树的前序遍历

前序遍历的递归解法: 方法一C++: 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 * }; 9 */ 10 class Solution { 11 public: 12 vector<

lintcode 容易题:Binary Tree Preorder Traversal 二叉树的前序遍历

题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递归实现,根节点->左节点->右节点 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(in

144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: 1 class Solution { 2 public: 3 vector<int> preorderTraversal(TreeNode* root) { 4 vector<int> ans; 5 if(root){ 6 TreeNode* temp; 7 stack<TreeNode*> s; //利用栈,

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

144 Binary Tree Preorder Traversal(二叉树的前序遍历)+(二叉树、迭代)

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

栈和递归的关系 144:Binary Tree Preorder Traversal

前序遍历:根左右 //用栈来实现非递归解法/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<int> preorderTraversal(T

Java for LeetCode 144 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]. 二叉树的前序遍历,根节点→左子树→右子树 解题思路一: 递归实现,JAVA实现如下: public List<Integer> preorderTraversal(TreeNode root) { List<Int

leetcode 144. Binary Tree Preorder Traversal ----- java

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? 求前序遍历,要求不用递归. 使用双向队列. /** * Definition for a b

LeetCode 144 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? 题目链接:https://leetcode.com/problems/binary-tre