Binary Tree Preorder Traversal -- leetcode

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?

算法一,栈

前序遍历。

1.訪问根结点

2.訪问左子树

3.訪问右子树

题目要求不使用递归。

此处使用栈实现。

/**
 * 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(TreeNode* root) {
        vector<int> ans;
        if (!root)
            return ans;
        stack<TreeNode *> s;
        s.push(root);
        while (!s.empty()) {
            root = s.top();
            s.pop();
            ans.push_back(root->val);
            if (root->right)
                s.push(root->right);
            if (root->left)
                s.push(root->left);
        }
        return ans;
    }
};

算法二,栈保存右子树结点。

和上面差别是,此处用栈仅仅保留右孩子结点。

算法一。事实上左孩子循环结束时刚刚入栈了,下次循环開始时,立刻又会出栈。

在此实现中。则直接用一变量保存左孩子。不必进栈出栈。

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ans;
        stack<TreeNode *> rights;
        while (root || !rights.empty()) {
            if (root) {
                ans.push_back(root->val);
                rights.push(root->right);
                root = root->left;
            }
            else {
                root = rights.top();
                rights.pop();
            }
        }
        return ans;
    }
};

算法三,线索遍历

不再使用栈。而使用节点中空暇的右指针。让其指向根结点。

訪问一个左子树之前。先找到其左子树最右下的孩子,让其右指针指向根结点。

以便在訪问完左子树后。能返回根结点。从而找到根结点的右子树。

即訪问左子树之前,须要先建立返回的线索。

要注意的是。在建立线索的情况下,在訪问一个结点时,假设其左子树不空。

则此时,包括两种情况:

1. 此结点未訪问过。

2. 此结点已经訪问过。即訪问完左孩子,刚延着线索返回来。

怎样区分上面两种情况。就是看左子树的返回线索是否已经建立。

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ans;
        while (root) {
            if (!root->left) {
                ans.push_back(root->val);
                root = root->right;
            }
            else {
                TreeNode *runner = root->left;
                while (runner->right && runner->right != root)
                    runner = runner->right;

                if (!runner->right) {
                    ans.push_back(root->val);
                    runner->right = root;
                    root = root->left;
                }
                else {
                    runner->right = NULL;
                    root = root->right;
                }
            }
        }
        return ans;
    }
};
时间: 2024-08-04 23:22:53

Binary Tree Preorder Traversal -- leetcode的相关文章

Binary Tree Preorder Traversal leetcode 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? 题解: 递归做法如下: 1     public void helper(Tree

Binary Tree Preorder Traversal (leetcode 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? Show Tags Show Similar Problems 可以递归,可以用栈 /

Binary Tree Preorder Traversal —— LeetCode

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? 题目大意:非递归方式实现二叉树的前序遍历. 解题思路:自己实现一个栈,循环(根节点直接入栈并

Binary Tree Preorder Traversal -- LeetCode 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]. Solution 1: (递归) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * Tre

[LeetCode][JavaScript]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? https://leetcod

[LeetCode 题解]: 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? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

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}.