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  *     TreeNode *left;
 7  *     TreeNode *right;
 8  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 9  * };
10  */
11 class Solution {
12 private:
13         vector<int> res;
14
15 public:
16     vector<int> preorderTraversal(TreeNode* root) {
17         foo(root);
18         return res;
19     }
20     void foo(TreeNode* root){
21         if(root == NULL)
22             return;
23
24         res.push_back(root->val);
25         foo(root->left);
26         foo(root->right);
27     }
28 };
29
30 // InOrder
31 class Solution {
32 public:
33     vector<int> inorderTraversal(TreeNode* root) {
34         if(root == NULL){
35             return vector<int>();
36         }
37         vector<int> result; // 创建结果
38         stack<TreeNode *> s;// 创建堆栈
39         TreeNode *p = root; // 临时让p为root
40         // 寻找p最左边的node
41         while(p!=NULL){
42             s.push(p); // 从root开始,将左边的node推入stack
43             p = p->left;// 更新p为左node
44         }
45         // s 为全是左节点的stack
46         // 对 s进行循环操作
47         while(!s.empty()){
48             // 将最最左边的,推入stack
49             p = s.top();
50             result.push_back(p->val);
51             s.pop(); // 自己消失了
52             // 然后观察这个的右边node
53             if(p->right != NULL){
54                 p = p->right;
55                 while(p!=NULL){ //观察node的左边
56                     s.push(p);
57                     p = p->left;
58                 }
59             }
60         }
61         return result;
62     }
63 };
64
65 // PostOrder
66
67 class Solution {
68 public:
69     vector<int> postorderTraversal(TreeNode* root) {
70         if(root == NULL){
71             return vector<int>();
72         }
73         vector<int> result;
74         stack<TreeNode *> s;
75
76         s.push(root);
77         while(!s.empty()){
78             TreeNode *temp = s.top();
79             result.push_back(temp->val);
80             s.pop();
81             if(temp->left!=NULL){
82                 s.push(temp->left);
83             }
84             if(temp->right!= NULL){
85                 s.push(temp->right);
86             }
87         }
88         reverse(result.begin(), result.end());
89         return result;
90
91     }
92 };

原文地址:https://www.cnblogs.com/kykai/p/11618819.html

时间: 2024-10-09 06:38:28

LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal的相关文章

[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,

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder 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? 不使用递归前序遍历,可以

LeetCode之“树”:Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder 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? 递归解

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

Binary Tree Preorder Traversal and Binary Tree Postorder 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]. c++版: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode

Binary Tree Postorder Traversal &amp;&amp; Binary Tree Preorder Traversal

详见:剑指 Offer 题目汇总索引:第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]. Note: Recursive solution is trivial, could y

【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

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]. 解题思路: 方法一:递归方法... class Solution {public: vector<int> postorderTraversal(TreeNode* root) { vector<int>