[LintCode] 二叉树的前序遍历

简介: The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and O(n) space): 1 /** 2 * Definition of TreeN...

The recursive solution is trivial and I omit it here.

Iterative Solution using Stack (O(n) time and O(n) space):

 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 
14 class Solution {
15 public:
16     /**
17      * @param root: The root of binary tree.
18      * @return: Preorder in vector which contains node values.
19      */
20     vector<int> preorderTraversal(TreeNode *root) {
21         // write your code here
22         vector<int> nodes;
23         TreeNode* node = root;
24         stack<TreeNode*> right;
25         while (node || !right.empty()) {
26             if (node) {
27                 nodes.push_back(node -> val);
28                 if (node -> right)
29                     right.push(node -> right);
30                 node = node -> left;
31             }
32             else {
33                 node = right.top();
34                 right.pop();
35             }
36         }
37         return nodes;
38     }
39 };

Another more sophisticated solution using Morris Traversal (O(n) time and O(1) space):

 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 
14 class Solution {
15 public:
16     /**
17      * @param root: The root of binary tree.
18      * @return: Preorder in vector which contains node values.
19      */
20     vector<int> preorderTraversal(TreeNode *root) {
21         // write your code here
22         vector<int> nodes;
23         TreeNode* node = root;
24         while (node) {
25             if (node -> left) {
26                 TreeNode* predecessor = node -> left;
27                 while (predecessor -> right && predecessor -> right != node)
28                     predecessor = predecessor -> right;
29                 if (!(predecessor -> right)) {
30                     nodes.push_back(node -> val);
31                     predecessor -> right = node;
32                     node = node -> left;
33                 }
34                 else {
35                     predecessor -> right = NULL;
36                     node = node -> right;
37                 }
38             }
39             else {
40                 nodes.push_back(node -> val);
41                 node = node -> right;
42             }
43         }
44         return nodes;
45     }
46 };

 

目录
相关文章
|
8月前
|
Java C++ Python
leetcode-144:二叉树的前序遍历
leetcode-144:二叉树的前序遍历
46 0
|
8月前
|
存储
LeetCode———144—— 二叉树的前序遍历
LeetCode———144—— 二叉树的前序遍历
|
8月前
|
存储
LeetCode题94,44,145,二叉树的前中后序遍历,非递归
LeetCode题94,44,145,二叉树的前中后序遍历,非递归
67 0
|
算法 索引
每日一题:LeetCode-105.从前序遍历与中序遍历构造二叉树
每日一题:LeetCode-105.从前序遍历与中序遍历构造二叉树
|
算法 Java
代码随想录训练营day14|144. 二叉树的前序遍历 145. 二叉树的后序遍历 94. 二叉树的中序遍历...
代码随想录训练营day14|144. 二叉树的前序遍历 145. 二叉树的后序遍历 94. 二叉树的中序遍历...
LeetCode——二叉树的非递归遍历
LeetCode——二叉树的非递归遍历
leetcode144.二叉树的前序遍历
leetcode144.二叉树的前序遍历
85 0
【LeetCode】-- 144. 二叉树的前序遍历
【LeetCode】-- 144. 二叉树的前序遍历
【LeetCode】-- 144. 二叉树的前序遍历
|
存储 程序员
【Leetcode】KY11 二叉树遍历(牛客网)、144. 二叉树的前序遍历、94. 二叉树的中序遍历、145. 二叉树的后序遍历
【Leetcode】KY11 二叉树遍历(牛客网)、144. 二叉树的前序遍历、94. 二叉树的中序遍历、145. 二叉树的后序遍历
107 0
【Leetcode】KY11 二叉树遍历(牛客网)、144. 二叉树的前序遍历、94. 二叉树的中序遍历、145. 二叉树的后序遍历
|
算法 Java
算法打卡Day17_leetcode _144. 二叉树的前序遍历
算法打卡Day17_leetcode _144. 二叉树的前序遍历
算法打卡Day17_leetcode _144. 二叉树的前序遍历