[LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal

前中后遍历 递归版


 1  /*  Recursive solution */
2 class Solution {
3 public:
4 vector<int> preorderTraversal(TreeNode *root) {
5
6 vector<int> result;
7 preorderTraversal(root, result);
8
9 return result;
10 }
11
12 void preorderTraversal(TreeNode *root, vector<int>& result)
13 {
14 if(root == NULL) return;
15 result.push_back(root->val);
16
17 preorderTraversal(root->left, result);
18 preorderTraversal(root->right, result);
19 }
20
21 };


 1  /*  Recursive solution */
2 class Solution {
3 public:
4 vector<int> inorderTraversal(TreeNode *root) {
5
6 vector<int> result;
7 inorderTraversal(root, result);
8
9 return result;
10 }
11
12 void inorderTraversal(TreeNode *root, vector<int>& result)
13 {
14 if(root == NULL) return;
15
16 inorderTraversal(root->left, result);
17 result.push_back(root->val);
18 inorderTraversal(root->right, result);
19 }
20
21 };


 1  /*  Recursive solution */
2 class Solution {
3 public:
4 vector<int> postorderTraversal(TreeNode *root) {
5
6 vector<int> result;
7 postorderTraversal(root, result);
8
9 return result;
10 }
11
12 void postorderTraversal(TreeNode *root, vector<int>& result)
13 {
14 if(root == NULL) return;
15
16 postorderTraversal(root->left, result);
17 result.push_back(root->val);
18 postorderTraversal(root->right, result);
19 }
20
21 };

下面是迭代版本

1 preorder: 节点入栈一次, 入栈之前访问。

2 inorder:节点入栈一次,出栈之后访问。

3 postorder:节点入栈2次,第二次出战后访问。


 1 class Solution {
2 public:
3 vector<int> preorderTraversal(TreeNode *root) {
4
5 vector<int> result;
6 stack<TreeNode*> stack;
7
8 TreeNode *p = root;
9
10 while( NULL != p || !stack.empty())
11 {
12 while(NULL != p)
13 {
14 result.push_back(p->val);
15
16 stack.push(p);
17 p = p->left;
18 }
19
20 if(!stack.empty())
21 {
22 p= stack.top();
23 stack.pop();
24
25 p=p->right;
26 }
27 }
28
29 return result;
30 }
31
32 };


 1 class Solution {
2 public:
3 vector<int> inorderTraversal(TreeNode *root) {
4
5 vector<int> result;
6 stack<TreeNode*> stack;
7
8 TreeNode *p = root;
9
10 while( NULL != p || !stack.empty())
11 {
12 while(NULL != p)
13 {
14 stack.push(p);
15 p = p->left;
16 }
17
18 if(!stack.empty())
19 {
20 p= stack.top();
21 stack.pop();
22
23 result.push_back(p->val);
24
25 p=p->right;
26 }
27 }
28
29 return result;
30 }
31
32 };

后续, 用bStack标记是否是第一次访问,如果是第一次访问,再次入栈,否则直接访问,记得将P = NULL;


 1 class Solution {
2 public:
3 vector<int> postorderTraversal(TreeNode *root) {
4
5 vector<int> result;
6 stack<TreeNode*> stack;
7 std::stack<bool> bStack;
8
9 TreeNode *p = root;
10 bool isFirst;
11
12 while( NULL != p || !stack.empty())
13 {
14 while(NULL != p)
15 {
16 stack.push(p);
17 bStack.push(false);
18 p = p->left;
19 }
20
21 if(!stack.empty())
22 {
23 p= stack.top();
24 stack.pop();
25
26 isFirst = bStack.top();
27 bStack.pop();
28
29 if(isFirst == 0)
30 {
31 stack.push(p);
32 bStack.push(true);
33 p=p->right;
34 }
35 else
36 {
37 result.push_back(p->val);
38 p = NULL;
39 }
40
41 }
42 }
43
44 return result;
45 }
46
47 };

[LeetCode] Binary Tree Preorder/Inorder/Postorder
Traversal

时间: 2024-10-11 06:09:18

[LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal的相关文章

LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal

题目描述 144. Binary Tree Preorder Traversal 94. Binary Tree Inorder Traversal 145. Binary Tree Postorder Traversal 前序排列 :根-左-右 中序排列: 左-根-右 后序排列:左-右-根 参考答案 1 // PreOrder 2 /** 3 * Definition for a binary tree node. 4 * struct TreeNode { 5 * int val; 6 *

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

LeetCode: Binary Tree Preorder Traversal [144]

[题目] 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? [题意] 非递归返回先序遍历结果 [思路] 维护一个栈即可 [代码] /** *

[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 Zigzag Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between

[leetcode]Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

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 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 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively 解题思路: 二叉树的前序遍历.